This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
/* My question is not regrading to equals or == when i compile and run following code it shows output S1 equals S2 true false where is S1==S2 string goes it should display following output S1 equals S2 true S1 == S2 false
if i uncommented line 8 and then compile following error occurs
Incompatible type for boolean. Can't convert boolean to java.lang.String. System.out.println(s3==s4+"S1 == S2 "+s1==s2); //line 8 ^
what im thinking in second case it first evalute and execute first == state ment then try to compare boolean value with String i.e s2 is compare against result of s3==s4 . but whatever case problem is that it is ignoring String in" "
Yogesh, I tried ur code....... the output is same as urs (// line 8 commented) S1 equals S2 true false But when I changed this line System.out.println("S1 == S2 "+s1==s2); as System.out.println("S1 == S2 "+(s1==s2)); // put brackets then I get this o/p S1 equals S2 true S1 == S2 false put brackets in line 8 & u'll get the expected results. Thx Aruna [This message has been edited by Aru Ven (edited November 02, 2000).]
Joined: Aug 31, 2000
i know that on using() i will get result but why not without using it String should be printed in all cases whether u use brackets or not there is some other reason i m unable to found that in JLS ..
Hi yogesh.The arithmetic operators have higher precedence than comparison operators.So when u say 1) String s4 is getting added to the string "s1==s2" 2) String s1 is getting added to the result of step 1 above. Thus we have a new String which is "HelloS1==S2Hello".This String is compared with String s1.Predictably,this gives false , which is a boolean value.Since == cannot be applied to a boolean value,hence the exception. I hope i have been clear enough. ------------------ Come on in !! Drinks are on the house in the Big Moose Saloon !!
Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
Joined: Aug 31, 2000
thanks to all of u i got it very nicely
Joined: Oct 18, 2000
Mr Gautam Shah, I understand your point.But my point is that first solve the basic question ie. operator precedence.If the user wants a intro. on compiler mechanics , i'm sure he will ask for it.Firstly let him clearly understand operator precedence.Compiler quirks and dynamics can come later. And i'm sure that we all have some pieces of code that can put the best of Java professionals into a tizzy.
------------------ Come on in !! Drinks are on the house in the Big Moose Saloon !!