Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String

 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really got the point.
Thanks for your time.
regards
vievk
 
Adrian Ferreira
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg,
Great! Now it sounds better to me.
Thanks,
Adrian
 
Hari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hoop!.

I got It, Thanks for your question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic