Thursday, April 29, 2010

Here comes one more Java puzzle for you...

I am up with my new puzzle today. Just go through the code below and see if you can guess the output. The code looks pretty simple. But beware, its not that easy:

public class StringPuzzle { 
 public static void main(String[] args) { 
   final String a = "length is 12"; 
   final String b = "length is " + a.length(); 
   System.out.println("Strings are equal: " + a == b); 
 } 
}

Well... can you guess the answer? Ok... let me read your mind... you guessed the output  Strings are equal: true or you might have guessed Strings are equal: false.

Well I have some bad news for you. In both the cases you are incorrect. Isn't it strange! Don't believe my words. Just execute this program and you will see it yourself. 

Post a solution in comment section. Happy coding!!

5 comments:

anjan said...

+ binds before == therefore the answer was false.

nice one.

ravi said...

Hi Arpit!
sorry to comment here.. but I was trying your application of ContactDemoApplication....of developing a demo application on netbeans.. I m using netbeans 6.8 with glassfish v3... everything is workin fine.. but on running the app. i m not getting the list of contacts added instead I m getting "Result = [org.webservices.client.Contact@2bc61a, org.webservices.client.Contact@1dad926, org.webservices.client.Contact@e6882f, org.webservices.client.Contact@f6f23b]"

ravi said...

Please help me ASAP..plz u can mail me as well...ravi.iiitm06@gmail.com

ZeeK said...
This comment has been removed by the author.
ZeeK said...

good catch!!