This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/*
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
}
}
 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
yogesh sood
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to all of u i got it very nicely
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic