aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == test of String literals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== test of String literals" Watch "== test of String literals" New topic
Author

== test of String literals

Vikram Reddy
Greenhorn

Joined: Jan 06, 2003
Posts: 17
Which of the following statements will evaluate to true?
A. "String".replace('g','G') == "String".replace('g','G')
B. "String".replace('g','g') == new String("String").replace('g','g')
C. "String".replace('g','G')=="StrinG"
D. "String".replace('g','g')=="String"
The answer is D. (verified by compiling and running)
Can anyone explain the result.
Thanks in advance,
Vikram


SCJP-1.4; SCWCD-EE5<br />---------<br />Sky is not the limit......
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12836
    
    5
Look at the source code for the replace method in java.lang.String to discover your answer.
Bill
Sharda Govindu
Greenhorn

Joined: Feb 13, 2003
Posts: 8
Hi,
source code of String class's replace() method:
"public String replace(char oldChar, char newChar)"
Returns a new string resulting from replacing all occurences of oldChar in this string with newChar.
In that case, C and D both should evaluate to true.
Is that correct??
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Sharda Govindu:
In that case, C and D both should evaluate to true.
Is that correct??

C does not evaluate to true because the left and right operands of the equality operator are references to different instances of strings that contain similar data. D does evaluate to true because the left and right operands are both references to the same String constant. The replace method returns the old instance of the String if both arguments are the same char.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Good question!
and welcome to the Ranch Vikram
[ February 28, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Sharda Govindu
Greenhorn

Joined: Feb 13, 2003
Posts: 8
Thanks Dan for clarifying!
Vikram Reddy
Greenhorn

Joined: Jan 06, 2003
Posts: 17
Thanks Jose... Dan...
Arnold Kesselaar
Greenhorn

Joined: Feb 28, 2003
Posts: 14
I read somewhere that all (or nearly) operations on strings return New string objects EXCEPT the replace where nothing is replaced. That could explain why only d is correct.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Arnold Kesselaar:
I read somewhere that all (or nearly) operations on strings return New string objects EXCEPT the replace where nothing is replaced. That could explain why only d is correct.

The String.replace method is not the only method that will sometimes return the current String instance. For example, the String.subString method will return the current String when the beginIndex argument is zero and the endIndex argument is equal to the String length. The String.concat method returns the current string instance if the length of the String argument is zero. In general, if the String method doesn't need to change anything then you should suspect that the returned String is the current String.
Arnold Kesselaar
Greenhorn

Joined: Feb 28, 2003
Posts: 14
Right you are. Thanx
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12836
    
    5
Which is why I said "look at the source code" - your SDK download comes with src.zip or src.jar - the complete source code to the basic Java library in compressed form. You should learn to use that to answer questions such at this, it will improve your understanding of Java much more than just getting a quick answer.
Bill
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401

You mean look at the implementation? What if (I know it's unlikely), but what if the implementation did actually change?

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.

(italics mine)
Nowhere does the docs say "If no replacement is necessary, then the original string is returned."
Now according to the Javadoc, replace() should return a new string unless oldChar does not occur in the character sequence.

Am I wrong?


comp.lang.javascript FAQ: http://jibbering.com/faq/
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

Nowhere does the docs say "If no replacement is necessary, then the original string is returned."

Here it says that:

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.

All the String methods that seem to modify a string behaves in the same way: they return the object on which they were inkoked if they happen to perform no process.

"String".replace('g','g') == "String"; // should be false.
"String".replace('g','g') == "String".replace('g','g'); // should be false.

They yield true however. The trick here is that the replace method will not perform a replacement of an argument by itself. It is clever enough to know beforehand the result of such a substitution and it will return the same string object (no process is necessary).
[ March 01, 2003: Message edited by: Jose Botella ]
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
So I guess the replace method has something like this:
If it's true (and so it appears to be), then the Javadoc should say this. I really should look instead of guessing, though.

All the String methods that seem to modify a string behave in the same way: they return the object on which they were invoked if they happen to perform no process.

Thank you, I'll keep that bit of knowledge. This includes aString.toString(), i]aString[/i].concat("").
What about "UPPER".toUpperCase(), "lower".toLowerCase(), "_=?\"".toLowerCase(), or "_=?\"".toLowerCase(Locale.ENGLISH) ?
This is probably getting a little away from the exam coverage of strings, I hope.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The following prints true.
System.out.println("UPPER"=="UPPER".toUpperCase());
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: == test of String literals