• 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

Difference between these two statement

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
What is the difference between
String s ="Hello"
&
String s= new String("Hello")

??

I am asking this question because in some forum i read in 2nd case two objects are created. If that is true , than how ??
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

because Java creates a special pool for String literals there are actually two objects created:

1) "Hello" as a string literal in the pool
2) The String object you created with new and assigned it to s

Marco
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings in java get special treatment. I suggest you search around on this site for threads that discuss the "String pool".

in BRIEF, the first time any string literal appears in the code, a String object is created in the String pool.

so when you say "new String("Hello")", the literal creates an object in the pool, and the 'new' operator creates the second object in the heap.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You all ...
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could some one also confirm if this is correct please?

String A = "hello";

String B = "hello";

As I understand it Java will create a referance from A to B (pointer) and only store "hello" once with muliple referances and not actually create two new areas of memory that store "hello"

Is that correct?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Stuart

Thats correct.

More details at the following link:

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the compiler will definitely not create a reference from A to B unless you assign B to A.

I think you mean something else. In this case the compiler creates multiple objects and variables. It creates a normal String object for "hello" which resides on the heap as any other object.
Besides because "hello" is known as a String literal at compile time the compiler creates a reference to it in the string literal pool. This is an optimization of the compiler/JVM special to strings.
And finally there will be two reference variables A and B which point to the same String "hello" which on the heap. I think it was this you were talking about. There's only one object on the heap because A and B use the same string literal.

Marco
 
Stuart Smith
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys that clears that up very well
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that, although perfectly legal, it is very rarely necessary or appropriate to use new String("something") constructor. If you are indeed a beginner, you are very unlikely to be in one of those rare situations.

For beginners(*), the semantics of new String("something") are of academic interest only, not practical use.


(*) and most of the time for others, too
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marco Ehrentreich:
Hi,

because Java creates a special pool for String literals there are actually two objects created:

1) "Hello" as a string literal in the pool
2) The String object you created with new and assigned it to s

Marco



I went through http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html

For the statement,
String s= new String("Hello");

I think the above 2 points contradict in the manner that 2 objects are created on heap and for new operator the object of one is returned to s. So, reference for this should be there in String Pool and another object which is on the heap should be lost.

Now I am really confused. I tried searching for previous post but couldn't really resolves about this issue. Please throw some light on only the above statement.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you..
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivit,

don't worry, I think this is more or less confusing to everyone

As I finally understood the string handling in Java the two differences between Strings and "normal" objects are string literals (enclosed in double quotes) and the behavior of the operator "new".

String literals are only evaluated at compile time and for each a reference is placed in the string literal pool. This is only done by the compiler when no variables are involved (e.g. concatenated).

"new" always creates a new string object at runtime regardless of the string literal pool. This is the reason why you end up with TWO objects on the heap when you write

I'm not an expert why strings were implemented this way and in what situations you gain so much advantage of this. Perhaps the best idea is to not use "new" like Peter already said.

Anyone may correct me please if I told something wrong...

Marco
 
Ranch Hand
Posts: 121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Smith:
Could some one also confirm if this is correct please?

String A = "hello";

String B = "hello";



 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Marco Ehrentreich

Thanks that was really confusing in the beginning.
 
reply
    Bookmark Topic Watch Topic
  • New Topic