String object question

class test{

publicstaticvoid main(String[] args){

String temp1 ="Me";

String temp2 ="Me";

System.out.println(temp1.equals(temp2));

}

}

I wrote the above quickly to test if temp1 and temp2 were objects. Well it appears that they are because I could use the equals

method. Is this just a shortcut in java, and exactly the same thing as writing

String temp1 =new String("Me");

?

[793 byte] By [jellystonesa] at [2007-11-27 8:54:07]
# 1
no
BigDaddyLoveHandlesa at 2007-7-12 21:12:55 > top of Java-index,Java Essentials,New To Java...
# 2

> I wrote the above quickly to test if temp1 and temp2

> were objects. Well it appears that they are because I

> could use the equals

method.

temp1 and temp2 are variables whose values are references, not objects. They both refer to the same String object, because Java pools string literals.

> Is this

> just a shortcut in java, and exactly the same thing

> as writing

> String temp1 = new String("Me");

No. With the new String you're creating a new String object that's a copy of the one that's in the string pool. Without it--as in your first code--it just provides a reference to the existing String in the pool.

jverda at 2007-7-12 21:12:55 > top of Java-index,Java Essentials,New To Java...