• 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 objects

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whats the difference between explicitly creating a String object using new operator and creating an object by using String Literal ?
In both cases, memory addresses are assigned or not ?
What other difference are there ?
Regards,
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sam, I'll see what I can do to clear things up a little with Strings. When javac compiles your code for you, it creates a literal pool which is basically a collection of all of the literal values that you have written in your program. Included in this literal pool are all of the Strings that you included in your program. So if you had the String "Hello World" in a line of code as it is here:
System.out.println("Hello World");
It would be a String object in a literal pool. If the following line of code were to execute:
String s1 = "Hello World";
Java recognizes this String object as one that it already contains in the literal pool and so it points s to that String object.
If, however, you were to write this instead:
String s2 = new String("Hello World");
Then Java does not use the pre-existing String object that it has in the literal pool. Instead it creates a new String object and s2 now points to that place in memory.
Whew! What a pile of stuff to remember. To sum it up a little bit: without using the new operator s1 will point to an existing String object so that:
a = "Hello World";
b = "Hello World";
a==b will evaluate to true (they are referencing the same String object in memory).
However,
a = new String("Hello World");
b = new String("Hello World");
a==b will evaluate to false because a and b are referencing separate String objects in memory)
(== with object references like a and b evaluates if their references are equal not the equality of the values of their objects).
Let me know if that was too much all at once. If so I could break it down for you or even give you a full example.
Hope that helped.
Paul
 
Sam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Caudle:
Hi Sam, I'll see what I can do to clear things up a little with Strings. When javac compiles your code for you, it creates a literal pool which is basically a collection of all of the literal values that you have written in your program. Included in this literal pool are all of the Strings that you included in your program. So if you had the String "Hello World" in a line of code as it is here:
System.out.println("Hello World");
It would be a String object in a literal pool. If the following line of code were to execute:
String s1 = "Hello World";
Java recognizes this String object as one that it already contains in the literal pool and so it points s to that String object.
If, however, you were to write this instead:
String s2 = new String("Hello World");
Then Java does not use the pre-existing String object that it has in the literal pool. Instead it creates a new String object and s2 now points to that place in memory.
Whew! What a pile of stuff to remember. To sum it up a little bit: without using the new operator s1 will point to an existing String object so that:
a = "Hello World";
b = "Hello World";
a==b will evaluate to true (they are referencing the same String object in memory).
However,
a = new String("Hello World");
b = new String("Hello World");
a==b will evaluate to false because a and b are referencing separate String objects in memory)
(== with object references like a and b evaluates if their references are equal not the equality of the values of their objects).
Let me know if that was too much all at once. If so I could break it down for you or even give you a full example.
Hope that helped.
Paul



Thanx Paul, The explanation really helped.
I have explored other things.
Thanx again,
Regards,

 
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic