• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Rajani Sudhakar
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this piece of code carefully

__________________________________________________________

if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

___________________________________________________________
Answers

1.the code will compile an print "Equal".
2.the code will compile an print "Not Equal".
3.the code will cause a compiler error

Correct Answer : 2.the code will compile and print "Not Equal"

But the result String formed on the lefthand side and that of the righthand side seem to be equal..

Can anyone justify the correct answer?

Thanks in advance,
rajani.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compare the output from that code with this:



Did you figure it out yet? If not, check this out, as well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic