aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String" Watch "String" New topic
Author

String

Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277

Please explain these questions, I really don�t understand
Q#1
Read this piece of code carefully
if("String".replace('T','t') == "String")
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
Answer is 1.
Q#2
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
Answer is 2.

i would appreciate it.
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
The replace method of the String class will return the same string if no replacement occurs. Otherwise a newly constructed String is returned.
So in the first question, "String".replace('T', 't') will evaluate to be the original "String". The == test is then true because of the rule that only one string literal is shared amongst all literals with the same contents.
The second question demonstrates that if a replacement occurs, new strings are constructed. These newly constructed strings will always be unique.
Milind Kulkarni
Ranch Hand

Joined: Jun 01, 2000
Posts: 146
Hi Vivek,
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.
In the example 1:
When String".replace('T','t') is evaluated, char �T� is not found so it returns the same reference and == evaluates as True. �Equal� will be displayed.
In the Example 2:
A new string object is created and old char �g� is replaced by �G� and == evaluates as False and �Not Equal� will be displayed.
Hope this helps !!
Regards,
Milind
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
I really got the point.
Thanks for your time.
regards
vievk
Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
Sorry but,
"only one string literal is shared amongst all literals with the same contents."
To me, it would be easy to understand if this rule were true also for Q#2. Since both Strings have the same contents, it isn�t an exception for the rule? When this rule is valid?
Thanks,
Adrian
Greg Whelan
Ranch Hand

Joined: May 18, 2000
Posts: 52
Yes, the strings in Q2 have the same contents but they are not string literals. They are new strings (non-literals) that are constructed and then returned by the replace method.
There was some interesting discussion along the same lines here:
http://www.javaranch.com/ubb/Forum24/HTML/001463.html
Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
Greg,
Great! Now it sounds better to me.
Thanks,
Adrian
Hari
Greenhorn

Joined: Jun 09, 2000
Posts: 7
Hoop!.

I got It, Thanks for your question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String