Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mock question

 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the result of executing the following code?

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


The code will compile and print "Equal".
The code will compile and print "Not Equal".
The code will cause a compiler error.
The code will compile but will throw runtime exception.

what will be the output
 
Haibo Jia
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer should be "Not Equal".

if("String".replace('g','G') == "String".replace('g','G'))


The code above compare two reference to the different String object. Pls remember String is immutable Class, So "String".replace('g','G') will result in creating reference to a new String object with value of "StringG". Two times excution will create two different reference, so the result of comparison is the false. Hope explanation makes sense.
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks

("StrinG".replace('g','G') == "StrinG".replace('g','G'))
this is giving true since StrinG is not replace
please correct me if I am wrong
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Haibo...

I didn't understand why this is not equal...


if("String".replace('g','G') == "String".replace('g','G'))


The second replace doesn't make reference to the first reference either?
 
Haibo Jia
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alexsandra,

The two replace methods will create two objects refered by 2 ref, so compansion of ref result in false. Thax.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Haibo Jia !!

As you said it will return false so i got the point,
but why in the post by V Gala as shown below it is
said it returns true........although both the
statements are same?Is it i am making some mistake ?

posted by V Gala


i need some explanation for this.............

Thanks in advance......
 
Abdullah Mamun
Ranch Hand
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dhwani

String.replace() method returns a new object if any change happens, otherwise it returns the same object as it is.

Originally posted by dhwani mathur:

("StrinG".replace('g','G') == "StrinG".replace('g','G'))
this is giving true since StrinG is not replace
please correct me if I am wrong


So, you are absolutely right.
[ August 24, 2007: Message edited by: Al Mamun ]
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i was going through the above discussion when i stumbled upon this piece of code...
now i'm confused...

i ran this piece of code:



OUTPUT:

arit
amit
arit arit
false
true

Going by the above discussion, in this code , line 4 creates a new String Object with value 'arit'. Now when line 5 is executed shouldn't s3 point to the newly created 'arit' object?
Wont it see in the String pool and find that such a String already exists and point the new references s3 and s4 to it?

But why am i getting false as the result of line 8? i'm confused ... please explain...

Thanks in advance
Radha
[ August 25, 2007: Message edited by: Radhika ]
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
String s1 = new String("amit");
String s2 = "amit";
String s3 = new String("amit");
if(s1==s2)
ans is false
if(s1==s3)
ans is false
whwn we write s1.replace('m','r') it is eqivalent to
String var= new String("arit");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic