permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String related query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String related query" Watch "String related query" New topic
Author

String related query

Srinivas Kumar
Ranch Hand

Joined: Jul 14, 2005
Posts: 52
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 thought answer would be 1 because "StrinG" will exist in the String constant pool.
can anybody tell me whether "StrinG" will be placed on heap or in String constant pool?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
If you look at the source code for String.java, you can see what's happening.



So basically if no character needs to be replaced, then a reference to the current String is returned.

If a character is replaced, then a new String object is created.
Antonio Trapero
Greenhorn

Joined: Mar 24, 2007
Posts: 11
if new String object is returned, it will print "Not Equal", cause both Strings are not in pool. On the other hand, if both replace calls were replace('G','g') , replace would not return new objects, and both would continue in pool. In this case, "Equal" would be printed.
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

This is taken from section 3.10.5 of the Java langauge specification(second edition)


Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern.

Thus, the test program consisting of the compilation unit (�7.3):

package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }

and the compilation unit:

package other;
public class Other { static String hello = "Hello"; }

produces the output:

true true true true false true

This example illustrates six points:

* Literal strings within the same class (�8) in the same package (�7) represent references to the same String object (�4.3.1).
* Literal strings within different classes in the same package represent references to the same String object.
* Literal strings within different classes in different packages likewise represent references to the same String object.
* Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
* Strings computed at run time are newly created and therefore distinct.
* The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.



Actually, all the compile time constants(within the same package and class) are by default contained in the string constants pool but the runtime strings are not in the constants pool until you use String.intern(). Based on the implementation of the replace method(as Keith pointed out), your if condition will evaluate to true if the character can not be replaced, since it just return "this" however, if the character is replaced, the replace method will always return a dynamically generated non-interned string, so your if condition will return false.


apigee, a better way to API!
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Nitesh,

Thank you very much friend.

Explore Java.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Srinivasan thoyyeti:
Hi Nitesh,

Thank you very much friend.

Explore Java.


Anytime Srini ... keep exploring and sharing the exploration details!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String related query