Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are Strings exeception to the rule

 
Vinay HS
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Vinay HS
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks mike.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic