• 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

What is the difference in String a = "abc" and String a = new String("abc")

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Sir,


Can Any One tell me that what is the difference between following 2 lines ?



What is the difference above 2 lines ?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the search next time because then you would have found this
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String a = "abc";

May or may not create a new String object.
If a String object with the literal "abc" already exists the reference 'a' will only point to it.
Since String objects are immutable.

Where as,
String a = new String("abc");
will always create a new Sting object.

 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Wouter, I came to know the exact difference between these two type of instantiation..

So, String a="abc";

its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one...

Constant literal table can be cleared only if JVM is terminated...
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one...


Umm, no. The second format uses the literal too, so how do you figure it's less "expensive"?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
public static void main(String... args) {
String str="java";
String str1 = new String("java");
System.out.println(str.equals(str1));
System.out.println(str.hashCode()+"---"+str1.hashCode());
}
}

When i tested with this program, i got the output as

true
3254818---3254818

Two String object is "equal" and their hashcodes are also equal...

There is a fact .. if two objects are equal by equal() method, then their hashcodes are also equal... Implicitly its the only one object referred by the two reference variables... So, str and str1 reference variables refer to the same "java" string literal in the literal pool... right?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does that make the second form more efficient than the first?
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:So, str and str1 reference variables refer to the same "java" string literal in the literal pool... right?


To check the reference equality you should have used '==', and obviously it is going to return false as you have used the 'new' keyword.
So, both are not the same references.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ram Narayan.M wrote:So, String a="abc";
its a costly one since object always persist in heap... If many string literals are created like this, Program will be running out of memory... Second format is best comparing to first one....


is a compile time constant and it will be loaded into the object heap as well as referenced from the string literal pool, but the second one will store the string in the object heap again - it wont take the already available reference in the pool,just because you have used 'new'.
So, see for yourself which one seems to be more efficient in terms of memory.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In worst case scenario..

What if there are large number of string literals referred by the String Litreal Pool?... It stays for long time and leads to the heap
memory space unavalability. So when the JVM is terminated, the string literal pool entries will be cleared...

If its String a= new String("abc");

Ok.. each time "abc" string object is created in the heap due to "new" keyword...

When the reference to the "abc" object is not there, it will be marked for removal by GC... But not in the case of compile time String literals..
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah...4 days have passed by! Just forgot this thread. Did some deep googling on Strings.. Now,i'm pretty much clear. Hope this helps you too.
Ram,
See if this is clear.
Anything within a double quotes is a String literal.

would actually create 2 objects & return 1 reference back. Since "hello" is a String literal,the compiler will make a note of it at compile time and will create a String object in the object heap & the reference is stored in the String literal pool when the Class is loaded for the very first time. This is followed by a creation of another new String object with exactly the same content "hello"(because of new String()). The reference of this second newly created String object in the heap is returned back to str.
If the String str goes out of scope or assigned null, it will be garbage collected sometime or the other.
The problem being, the first reference(of the literal) will continue to exist as long as the JVM runs. It will never be unreferenced - the literal pool holds the reference,remember?

So, again I leave it to you to decide which of the 2 is more efficient.

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Priety sharma. "" String a = "abc";

May or may not create a new String object.
If a String object with the literal "abc" already exists the reference 'a' will only point to it.
Since String objects are immutable. ""

String s1="ABC";
String s2="ABC";
If above case is true,then

s1==s2 should be used to check equality as both variables points to same memory location ,but it will not Work so we have to use "equals" Method. Why this happens??
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Actually
String s1 = "abc";
does not create a new String. The String "abc" is created when the class is loaded, if it does not already exist from somewhere else.

You should only use == for
  • 1: Comparison with null.
  • 2: Comparison of true singletons, i.e. enum elements.
  • 3: When writing equals methods.
  • 4: If you simply want to find out what happens if you use it.
  • String s1 = "abc";
    String s2 = "abc";

    will set both references equal to the already existing String "abc". So both s1.equals(s2) and s1 == s2 will evaluate to true.

    You realise this is a 5‑year‑old thread? You will find all the information you need in the link Wouter Oet posted.
     
    I do some of my very best work in water. Like this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic