File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Are Strings exeception to the rule 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 » Java » Beginning Java
Bookmark "Are Strings exeception to the rule" Watch "Are Strings exeception to the rule" New topic
Author

Are Strings exeception to the rule

Vinay HS
Greenhorn

Joined: Feb 25, 2005
Posts: 5
In java when two reference variables holding identical data (different instances) are compared with == operator, the answer would be false. But this is slightly different in case of String. You can see that when you run the following program.

In case we use 'new' operator and create an instance the string variable acts as 'Reference type'.

When a string literal is assigned to a String variable and then compared with String literal it acts as 'Primitive type'.

Why is this so ? Isn't ( String s3 = "hi"; ) a way of instantiating a string ? Or does it altogether have a different semantics ?
----------------------------------------------------------------------------
class Temp {
public static void main(String[] args){
String s1 = new String("hi");
String s2 = new String("hi");
String s3 = "hi";
System.out.println("Value of S1:" + s1);
System.out.println("Value of S2:" + s2);
System.out.println("Value of S3:" + s3);

if(s1 == "hi")
System.out.println("Passed-1");
if(s1 == s2)
System.out.println("Passed-2");
if( s3 == "hi")
System.out.println("Passed-3");
}
}

OUTPUT:
Value of S1:hi
Value of S2:hi
Value of S3:hi
Passed-3
Tim Wilson
Greenhorn

Joined: Feb 25, 2005
Posts: 2
When you compile your code, the two references to the constant "hi" will refer to the same String object.

At runtime, you are creating new String objects for s1 and s2, and you are passing in the "hi" constant that is referred to by s3.

Hope that helps.

Tim.
Vinay HS
Greenhorn

Joined: Feb 25, 2005
Posts: 5
Thats right Tim. Tell how is this implemented. when reference types are compared we compare the value stored in them(which is address of the objects). Is { s3="hi" } handled same as { s1=new String("hi") }. I Know for the later memory is allocated in heap.

How is s3="hi" stored?
Is "hi" stored in symbol table as a literal, and s3 has a reference of the symbol table entry. In that case camparing {if (s3=="hi")} would result in true as both would refer to same entry in symbol table.
(I could think of this as theway of implementation. Is it so?)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Is "hi" stored in symbol table as a literal

I don't know where String literals are stored during compilation, but during execution String literals are stored in the String Literal Pool. The String reference variables refer to the String objects in the pool. Since a String literal with any given sequence of characters is only put in the pool once, you get the result Vinay described.
[ February 25, 2005: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
Vinay HS
Greenhorn

Joined: Feb 25, 2005
Posts: 5
Thanks mike.
 
wood burning stoves
 
subject: Are Strings exeception to the rule