aspose file tools*
The moose likes Java in General and the fly likes Any one explain the following one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Any one explain the following one" Watch "Any one explain the following one" New topic
Author

Any one explain the following one

Sreenu Vengala
Greenhorn

Joined: Oct 26, 2009
Posts: 18
Hi everyone.

anyone explain the folowing

1. String strNameOne = "coderanch";

2. String strNameTwo = new String("coderanch");

anyone explain difference between 1 & 2.

Thanks&Regards
Srinu

Venkata Kumar
Ranch Hand

Joined: Apr 16, 2008
Posts: 110

The question is already answered in different post.
Please see the link http://www.coderanch.com/t/485716/Java-General/java/Creating-new-Strings#2182216


SCJP 5.0, SCWCD 5, preparing for SCDJWS
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
They compile to the same end result. 1) is just a shorter way
to enter 2) - less typing - 12 characters less.

Jim ... ...


BEE MBA PMP SCJP-6
Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 97
There is a strong difference between the two.

1. String strNameOne = "coderanch";
In this case you are creating a string reference in the literal pool.
And the main issue here is that the String created does not get garbage collected.
Reason: suppose you make strNameOne = null;
In this case you would have released the reference from the local variable table.
But still, there will be one reference existing from the literal pool table to this object "coderanch".
And so the object will not be ready for GC.

2. String strNameTwo = new String("coderanch");
In this case, you will have only one reference from the local variable table.
There will be no reference from the literal pool table.
So, if you make strNameTwo = null;
In this case, you would have released the reference from the local variable table.
So, the object "coderanch" will be ready for GC.

All the more....
In case1, if another object is created the same way... like....
String string3 = "coderanch";
string3 will also point to the same object "coderanch" as pointed by strNameOne variable.
Because, the JVM will look for the object created this way and if the object is already seen in the String Literal pool, then the variable will be pointed to the same object, else the object will be added to the heap and the reference will be put in the literal pool.

Any corrections, please point it out to me!!!

Regards,
Sriram
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

then the variable will be pointed to the same object, else the object will be added to the heap and the reference will be put in the literal pool.


I dont think reference will be put in literal pool.

Suppose we have


then JavaRanch literal will be in String Literal Pool. Object created will be in heap memory area and reference vairable str will be placed in heap/stack delending upon instance variale/local variable respectively.

Let me know in case this is not correct understanding.

Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
The string literal pool and intern() method are very interesting but
how is the pool used in practical situations?

Jim ... ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any one explain the following one