• 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

String difference

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class StaticTest {

public static void main(String[] args) {

String str1="HI";
String str2 = new String("HELLO");

}

}

Hi ,
I would like to know the difference b/w str1 & str2 if any ...
( if both r reference then where is the object of str1, I mean without new operator how can we allocate memory in heap )
Thanks .
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first one: it creates one String object and one reference variable.

The secone one: it creates two String objects and one reference variable (String and literal objects and str2 refers to the String object).
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do u mean by literal object .
what is the use of that .
Every time when we create any object the literal object gets created .
Please help ?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sanjaya is referring to the String literal pool.

Consider the statement "String str2 = new String("HELLO");". The Java compiler sets up every String literal like "HELLO" in your source program in a special memory area called the "String literal pool". If your program used the literal "HELLO" somewhere else, there would still be only one String like this in the pool. The literal "HELLO" is supplied as an argument to the String() constructor and a new object is created at runtime. A reference to that new object is then stored in reference variable str2. So there are two objects, one in the String literal pool and another one in the same heap where all other objects are kept.

Now consider "String str1="HI";". Here, "HI" is in the String literal pool. A reference to "HI" in the pool is stored in str1. No other object is created.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike It was very good explanation .
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means it is always better to make String Object ( Actually reference )
like this :
String s = HI";
instead of :
String s = new String("HELLO");
because it will save the memory in heap ( no object in heap , just one object in literal pool ) .
Am I right ?

thanks
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are your Strings big enough to matter in a modern computer with megabytes or gigabytes of memory?

Do you mind giving up all hope of garbage collecting the Strings when you are done with them?

Does it matter to you that identical Strings in the String Literal Pool use the same memory while identical String objects on the heap each use separate memory?

Now you decide.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever is the Case ....
Saving is always better ....
Is there any loss with this in particular :
String s = "HI"

Reply, Thanks .
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Mike was making the point: "Why does it matter?".
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is there any loss with this in particular :
String s = "HI"



String s = "HI";
puts one sharable string in the String Literal Pool
String s = new String("HI");
puts a sharable string in the String Literal Pool and an unshared string on the heap. So the first form is better if your main issue is minimizing peak memory usage.
reply
    Bookmark Topic Watch Topic
  • New Topic