Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rakesh Basani
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Kedar Pethe
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • 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!!
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Pritish Chakraborty
Ranch Hand
Posts: 91
C++ Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • 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 .
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • 1
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic