• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mock question

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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......
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
reply
    Bookmark Topic Watch Topic
  • New Topic