Wednesday, September 23, 2009

One reason to put braces in IF statement

Recently it happened to read adam-bien blog. I found the interesting explanation for why curly braces blocks are required for IF statements in Java.

Without curly brackets, you could accidentally write a semicolon after the IF-statements. The semicolon is a valid, empty statement, which will be "execute" instead of the actual (intended) one.

So either write the IF-statements with curly brackets blocks ...or carefully search for semicolons after the IF-statement.


Powered by ScribeFire.

Tuesday, August 11, 2009

Apple's mistakes on iPhone in India


Apple's iPhone is great phone with high memory, highest display clarity, smooth touch screen panel and many other compared to other phones available in India. It has already got a world wide community which delivered cool applications for iPhones. But it is yet to get a big sales attraction in India though it has been launched year ago.

Why apple fails? Doesn't care the sales in India? Why apple hesitates to land in India firmly? No official stores yet to open. I do not know what apple thinks or what business pressure it has with Indian mobile market. I feel the below

  1. It is price tagged huge (200$ in US should be priced below Rs 12,000. But now the 8GB model priced Rs.31,000 and 16GB is Rs.35,000 (approximately 660$ and 770$)
  2. India is a big market for mobile phones (more than 60% by Nokia). Indians can afford the cost if this is available in rest of the world. Phones with features that iPhone has are available in India. but they are(will be) expensive if apple quoted 12,000 rupees.
  3. India has huge GSM market and should not stick with operators. but iPhones are currently with Airtel and Vodafone. (I've changed this point as i didn't know this earlier)
  4. I do not think they understand the India mobile market clearly.
I like the Apple's strategy with iPod and iPhones. Keep the same name for the upgraded model and the applications have backward compatibility. This is missing in current Indian mobile market. Every phone behaves its own way and interestingly we have hundreds of model actively available. Few options apple can consider
  • Reduce the price as they can sell more.
  • Should not tie up with service operator.(I know that there are many to disagree with this point)
  • Apple can have more direct stores.
  • Design sales and marketing strategies FOR India

Wednesday, July 29, 2009

Accessing protected URL in Java

Come through an elegant code yesterday. Somehow the content needs to be accessed from the database server which is password protected. A simple URL query would fetch the content. I've tried Apache commons. I could managed to access the content via plain java.net classes works for me pretty smoothly.

here is how

Authenticator.setDefault(new MyAuthenticator("user","password"));

URL url = new URL("http://localhost:5300/database?_query='for $a in collection('test')/types return $a'");
InputStream resultStream =url.openStream();

And the simple inner class
class MyAuthenticator extends Authenticator {
private String username, password;

public MyAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

// This method is called when a password-protected URL is accessed
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
}

Are there any better way?

Monday, July 27, 2009

Returning null from methods in Java

It's best not to return null from a methods that returns an String type. instead return just empty string "". This will actually avoid extra checking in the calling method. Return variables can be created and initialized with empty (""). If any results constructed in the method, can be assigned to this result variable. if nothing constructed, it might fall on the last instruction that simply return the initialized variable.

Another approach can be using an Static String constant when empty string need to be requited.

It's best not to return null from a method that returns anarray type. Always returning an array, even if the array has zero length, greatly improves the generality of algorithms. If you
anticipate that your methods will return zero-length arrays frequently, you might be concerned about the performance implications of allocating many such arrays. To solve that problem, simply allocate a single array, and always return the same one, for example:
<code>private static final int ZERO_LENGTH_ARRAY[] = new int[0];</code>
This array is immutable (it can't be changed), and can be shared throughout the application.

We can return null in case of returning an Object.