• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

new String("Hello") Vs. String s="Hello"

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends..

We can create strings in java using
i) Stirng s=new String("Hello");
ii) String s="Hello";


What is the difference between these two forms of creation...

Thanks in advace
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In i) only one String object is created. This object will be placed in the String constant pool.
In ii) two objects are created. Object one- String constant "Hello" to be placed in the String constant pool, Object two- String object with value "abc"

Do this after the two statements add the statement below and see the output

Reason- All reference variables refer to the same "Hello" string in the String constant pool.
int hashCode() - returns Object ID number(may not be always unique)

Now change the value of s2 to "Hellllo" and the output is-
69609650 69609650 -1824599884



Hope this helps!!
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kedar Pethe wrote:In i) only one String object is created. This object will be placed in the String constant pool.
In ii) two objects are created. Object one- String constant "Hello" to be placed in the String constant pool, Object two- String object with value "abc"


Don't you mean the other way around? (and why "abc")

Kedar Pethe wrote:Reason- All reference variables refer to the same "Hello" string in the String constant pool.
int hashCode() - returns Object ID number(may not be always unique)


That's not the reason. hashCode() is overridden in the String class to be based on the value, so two different Strings with the same value wll always return the same hash code. If you want to check whether they're the same object, compare them using ==.
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be "Hello" instead of "abc", i mistyped!
No in i) one object is created, and ii) two objects are created!
You can refer K&B, Chapter 6: Strings.
About the hashCode, two different strings with the same value are represented by only one "Hello", two "Hello" 's are NOT present in the pool.
s,s1 and s2 will point to the SAME one "Hello" string in the pool.
 
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kedar, you need to reread that portion...

String s = new String(str) creates a String object on the heap with that value and also places the same value/object in the String literal pool. If already present, it does not need to place the value, and I do not know what it does then.

String s = str places the value in the String constant pool if it is not already there or redirects the reference variable to the same, if it is already present. Thus only one object is created, and *not* on the heap.

Your resilience is admirably outstanding, Kedar, but surely a forum Bartender would have some amount of knowledge, K&B not withstanding? :P
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kedar Pethe wrote:
About the hashCode, two different strings with the same value are represented by only one "Hello", two "Hello" 's are NOT present in the pool.


That wasn't my point though. My point is that that looking at the hash code alone won't tell you this. Two strings with the same value would have the same hash code regardless of where they are stored.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pritish Chakraborty wrote:Your resilience is admirably outstanding, Kedar, but surely a forum Bartender would have some amount of knowledge, K&B not withstanding? :P


Thanks for the support, but don't think we're infallible! I certainly make mistakes .
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rakesh Basani,
There are many threads already available on Code Ranch for your doubt.
You can get those through Search
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is when is it resolved. The String literal is a compile time constant and is resolved by the compiler. Hence, there is one reference to the String constant pool. The new String("") is resolved at runtime and hence a heap object is created which would be referred to the String constant pool object.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic