• Post Reply Bookmark Topic Watch Topic
  • New Topic

A String Question  RSS feed

 
avseq anthoy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why?
I think that "abc" had been created at line 1,so it is added to pool.Because String is immutable,x2 should reference the same object "abc" in pool created at line 1.But why == operator return false?
[ May 27, 2004: Message edited by: avseq antohy ]
 
Gian Franco
blacksmith
Ranch Hand
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think the result of the code snippet prints 'false', because
x2 is not a compile time constant and therefore with the
assignment

that follows, the result "abc" is not referencing the
same string in the literal pool.

If the x2 where a compile time constant as follows

(this is not a local variable of course), then the
following


Cheers,

Gian Franco Casula

[ May 27, 2004: Message edited by: Gian Franco Casula ]
[ May 27, 2004: Message edited by: Gian Franco Casula ]
 
avseq anthoy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excuse me
I don't know what is compile time constant.And why it can affect the result?

thanks for your answer^^
 
Sivakumar Swaminathan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
String Literals will represent different references if they are newly created at runtime and hence return false. If the compiler is able to compute string literals at compile time,then, it will represent same references(because it can be found in the string pool),and hence return true.That is why when we declare a static final String and do the same thing you did, we get true.

Hope it helps! Please see the sample code..

Sample Code Check :
public final static String x3="ab";
public static void main(String[] args){

String x1 = "abc";

String x2 ="ab";
x2 = x2 + "c";

String x4 = x3+"c";

String x5= x1;

//Case 1 println: returns false
System.out.println(x1==x2);

// Case 2 println : returns true because we are comparing canonical representations
System.out.println(x1.intern() == x2.intern());

//Case 3 println: returns true because literal string can be computed at compile time

System.out.println(x1==x4);

//Case 4 println oints to same reference,hence returns true
System.out.println(x1==x5);


}
 
Kali Praveen
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with shivkumar. Canonical representations give us the same references. For your example

System.out.println(x1 == x2.intern());

it will print true because x2.intern() will return the value of x2 in the pool.
 
avseq anthoy
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Thanks for yours explanation!
But I am so stupid that i can't understand that how to know the variable will be computed in compile time or runtime.

thx for replying^^
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!