• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Any one explain the following one

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 110
Firefox Browser MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is already answered in different post.
Please see the link https://coderanch.com/t/485716/Java-General/java/Creating-new-Strings#2182216

 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ... ...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic