difference between String s = "abc" and String s = new String("abc")
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sandeep_toraskar
Posted On:   Tuesday, June 29, 2004 05:41 AM

difference between String s = "abc" and String s = new String("abc")

Re: difference between String s = "abc" and String s = new String("abc")

Posted By:   Technically_Rational  
Posted On:   Tuesday, June 29, 2004 08:43 AM

String s = "abc" returns the reference of the value "abc" ( if exists) from the String pool from JVM. If there is no reference to "abc" in the String pool, it creates one. new of String() creates a new instance of String object.

Hence, if you compare the objects first-s and the second-s, they will be different. However first-s.equals( second-s ) will return true.

Re: difference between String s = "abc" and String s = new String("abc")

Posted By:   Ivo_Limmen  
Posted On:   Tuesday, June 29, 2004 06:37 AM

That is simple: none.

Technically speaking, when you write:

String s = "abc";

The compiler will rewrite your code to:

String s = new String("abc");

The first example was made possible to ease the developers, both options are equal and correct.
About | Sitemap | Contact