File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes new String( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "new String("Hello")  Vs. String s="Hello"" Watch "new String("Hello")  Vs. String s="Hello"" New topic
Author

new String("Hello") Vs. String s="Hello"

Rakesh Basani
Ranch Hand

Joined: Jun 04, 2012
Posts: 38
Hi friends..

We can create strings in java using
i) Stirng s=new String("Hello");
ii) String s="Hello";


What is the difference between these two forms of creation...

Thanks in advace
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
In i) only one String object is created. This object will be placed in the String constant pool.
In ii) two objects are created. Object one- String constant "Hello" to be placed in the String constant pool, Object two- String object with value "abc"

Do this after the two statements add the statement below and see the output

Reason- All reference variables refer to the same "Hello" string in the String constant pool.
int hashCode() - returns Object ID number(may not be always unique)

Now change the value of s2 to "Hellllo" and the output is-
69609650 69609650 -1824599884



Hope this helps!!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4384
    
    8

Kedar Pethe wrote:In i) only one String object is created. This object will be placed in the String constant pool.
In ii) two objects are created. Object one- String constant "Hello" to be placed in the String constant pool, Object two- String object with value "abc"

Don't you mean the other way around? (and why "abc")

Kedar Pethe wrote:Reason- All reference variables refer to the same "Hello" string in the String constant pool.
int hashCode() - returns Object ID number(may not be always unique)

That's not the reason. hashCode() is overridden in the String class to be based on the value, so two different Strings with the same value wll always return the same hash code. If you want to check whether they're the same object, compare them using ==.
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
It should be "Hello" instead of "abc", i mistyped!
No in i) one object is created, and ii) two objects are created!
You can refer K&B, Chapter 6: Strings.
About the hashCode, two different strings with the same value are represented by only one "Hello", two "Hello" 's are NOT present in the pool.
s,s1 and s2 will point to the SAME one "Hello" string in the pool.
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

Kedar, you need to reread that portion...

String s = new String(str) creates a String object on the heap with that value and also places the same value/object in the String literal pool. If already present, it does not need to place the value, and I do not know what it does then.

String s = str places the value in the String constant pool if it is not already there or redirects the reference variable to the same, if it is already present. Thus only one object is created, and *not* on the heap.

Your resilience is admirably outstanding, Kedar, but surely a forum Bartender would have some amount of knowledge, K&B not withstanding? :P


OCJP 6
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4384
    
    8

Kedar Pethe wrote:
About the hashCode, two different strings with the same value are represented by only one "Hello", two "Hello" 's are NOT present in the pool.

That wasn't my point though. My point is that that looking at the hash code alone won't tell you this. Two strings with the same value would have the same hash code regardless of where they are stored.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4384
    
    8

Pritish Chakraborty wrote:Your resilience is admirably outstanding, Kedar, but surely a forum Bartender would have some amount of knowledge, K&B not withstanding? :P

Thanks for the support, but don't think we're infallible! I certainly make mistakes .
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
Rakesh Basani,
There are many threads already available on Code Ranch for your doubt.
You can get those through Search
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

The point is when is it resolved. The String literal is a compile time constant and is resolved by the compiler. Hence, there is one reference to the String constant pool. The new String("") is resolved at runtime and hence a heap object is created which would be referred to the String constant pool object.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
 
 
subject: new String("Hello") Vs. String s="Hello"