aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes System.out.println(S1==S2)???????? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "System.out.println(S1==S2)????????" Watch "System.out.println(S1==S2)????????" New topic
Author

System.out.println(S1==S2)????????

yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
/*
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" "

pls help me


*/

class Com{
public static void main(String args[]){
String s1=new String("Hello");
String s2="Hello";
String s4="Hello";
String s3="Hello";
System.out.println("S1 equals S2 "+s1.equals(s2));
System.out.println("S1 == S2 "+s1==s2);
//System.out.println(s3==s4+"S1 == S2 "+s1==s2); //line 8
}
}

If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
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).]
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
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 ..
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
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
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
thanks to all of u i got it very nicely
Udayan Naik
Ranch Hand

Joined: Oct 18, 2000
Posts: 135
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 !!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: System.out.println(S1==S2)????????