• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

String Test

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would the IF condition be

true

in PROGRAM1 and print

false

in PROGRAM2.Please explain

PROGRAM1 UTPUT IS TRUE

PROGRAM2: OUTPUT IS FALSE
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: In program #2, the output is a single word, false. But wouldn't you expect it to be s1==s2: false? If you can figure out what's happening to the String literal "s2==s2: ", then you will see why it's behaving this way.
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that s1 and s2 are two string literals pointing to the same string in the pool. Comparing s1 and s2 with

==

operator in the IF CONDITION prints

TRUE

. But if I use the same comparision in the System.out.println(s1==s2), it prints

FALSE


WHY?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
string plus boolean
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Esther Kak:
...But if I use the same comparision in the System.out.println(s1==s2), it prints
FALSE


Ah, but...
System.out.println(s1==s2);
...is not the same as...
System.out.println("s1==s2: " + s1==s2);

Note that the second of these will output false. It will not output s1==s2: false. Do you see what's happening? (See Dan's hint above.)
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I knew it.By the way why did it take so long for me to figure that out.


THANKS for the HINT.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone please explain or give a hint of what's really happening to the String "s1 == s2: "?

I still don't understand how is it printing one word 'false', i was expecting "s1 == s2: " false.

Regards,
Siphiwe M
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Siphiwe Madi:
Hi,

Can someone please explain or give a hint of what's really happening to the String "s1 == s2: "?

I still don't understand how is it printing one word 'false', i was expecting "s1 == s2: " false.


Think about the relative precedence of the == operator and the + operator. What are the two operands of == in this code fragment?
How does this relate to the Program2 main() method in the original post?
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Think about the relative precedence of the == operator and the + operator. What are the two operands of == in this code fragment?

code:
--------------------------------------------------------------------------------

int a = 5;int b = 7;System.out.println(2+a==b);

--------------------------------------------------------------------------------

How does this relate to the Program2 main() method in the original post?



So the order of precedence of the == and the + operator is that the + operator has a higher level of precedence than ==?

I think the two operands of == in your code fragment is:
7 == 7 (which evaluates to true). (I hope I�m right.)

So what actually happens in Program2 main() is:

String s1 = �Compare�; line 1
String s2 = �Compare�; line 2
System.out.println(�s1 == s2: � + s1 == s2); // line 3

First s1 is concatenated to the string �s1 == s2: � + s1 therefore equaling to �s1 == s2: Compare�

Then the string �s1 == s2: Compare� is compared to the string s2, giving us the Boolean expression below:

System.out.println(�s1 ==s2: Compare� == s2) - which therefore returns the result of false.
Because the string �s1 == s2: Compare� is not equal to �Compare�.

Can someone please confirm my reasoning here?
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic