• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any one explain the following one

 
Sreenu Vengala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
Firefox Browser Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They compile to the same end result. 1) is just a shorter way
to enter 2) - less typing - 12 characters less.

Jim ... ...
 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The string literal pool and intern() method are very interesting but
how is the pool used in practical situations?

Jim ... ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic