• Post Reply Bookmark Topic Watch Topic
  • New Topic

String concepts in Java  RSS feed

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I thought that I have understood the String Pool concepts in Java completely and was confident about it till I came across this program below:



The output of ab==abc comes out to be false ... As I understand that strings are immutable in java .. so a+"deal" would make a new string literal "meowdeal" and ab would point to "meowdeal" ... now we have abc also pointing to "meowdeal" .... hence there should be only one "meowdeal" in string pool and both ab and abc should be pointing to that .... and hence ab==abc should return true ... while its returning false ...

I have been told that a+"deal" makes a new object in the heap similarly to String ab = new String("meowdeal") ... why so ? could anyone explain me this and give me some reference material to read on the same ... I am totally confused and my concepts are totally shattered ...... need some help in regaining my confidence on strings ....
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddharth Bhargava wrote:so a+"deal" would make a new string literal "meowdeal"


No, this is the flaw in your thinking. It does indeed make a new String value, but it doesn't make a new String literal. Recall that a String literal appears in your code as a quote followed by a series of characters followed by a quote; your example contains three such literals.

And yes, it's true that your example does contain a String literal (at line 8) which contains the same series of characters as that String value created at line 6. But there's nothing which tries to match String values to entries in the String pool. So the literal at line 8 will be in the String pool, as you know, but the value at line 6 won't.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Siddharth Bhargava wrote:so a+"deal" would make a new string literal "meowdeal"


No, this is the flaw in your thinking. It does indeed make a new String value, but it doesn't make a new String literal. Recall that a String literal appears in your code as a quote followed by a series of characters followed by a quote; your example contains three such literals.

And yes, it's true that your example does contain a String literal (at line 8) which contains the same series of characters as that String value created at line 6. But there's nothing which tries to match String values to entries in the String pool. So the literal at line 8 will be in the String pool, as you know, but the value at line 6 won't.


that's what I am asking why does it make a new String value (as in new String object = new String("meowdeal")) and not a string literal ? .... fine that String literal appears in code as a quote followed by series of characters followed by a quote .... but still dosn't a+"deal" make a new string literal "meowdeal" ? let me know which material to read on the net which help me convince and clears my concepts ....
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please check the String Docs at http://docs.oracle.com/javase/7/docs/api/java/lang/String.html. It suggests below

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

It seems JVM will convert String ab = a+"deal"; to String a = new StringBuilder(a).append("deal").toString(); and will create a new object.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddharth Bhargava wrote: .... fine that String literal appears in code as a quote followed by series of characters followed by a quote .... but still dosn't a+"deal" make a new string literal "meowdeal" ? let me know which material to read on the net which help me convince and clears my concepts ....

No, because a + "deal" is not: a quote followed by a series of characters followed by a quote.

It's as simple as that - a string literal is just a string that is literally in your source code, starting and ending with a quote. Nothing more and nothing less. Don't overthink it.
 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jesper,

I have 1 question regarding the same concept. If i say 2 strings,



So in this case temp1 will be in string pool and in 2nd case it creates one object in a heap and another one pointing to "hello" String present in the string pool (i.e temp1). Right?

I am talking about Java 6. I know in Java 7+ String pool doesn't exist.
 
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. The String referenced by temp2 may live either on the heap or on the stack, Java is free to make this decision if temp2 is not a field. It also doesn't point to the "hello" in the String pool but it may point to the same shared character array, which may live in the String pool.
 
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:No. The String referenced by temp2 may live either on the heap or on the stack, Java is free to make this decision if temp2 is not a field.


Have things changed on that front?
All objects were stored on the heap last I looked.
The stack only consisted of references and primitives.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So we can't tell where temp2 points to. It may create an object in heap or in string pool.
 
Dave Tolls
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:So we can't tell where temp2 points to. It may create an object in heap or in string pool.


No, it creates it on the heap.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Stephan van Hulst wrote:No. The String referenced by temp2 may live either on the heap or on the stack, Java is free to make this decision if temp2 is not a field.


Have things changed on that front?
All objects were stored on the heap last I looked.
The stack only consisted of references and primitives.


I can't say for certain, but I believe that under the right conditions Java may put objects on the stack after performing escape analysis, so that objects are automatically cleared after a method call. This was added in one of the later versions.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it:
Java HotSpot™ Virtual Machine Performance Enhancements wrote:Escape analysis is a technique by which the Java Hotspot Server Compiler can analyze the scope of a new object's uses and decide whether to allocate it on the Java heap.

Escape analysis is supported and enabled by default in Java SE 6u23 and later.

Link
 
Dave Tolls
Ranch Foreman
Posts: 3071
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Found it:


Ah, ta for that.
Always something "new" to learn.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I never knew that..
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that very short‑lived objects can be created on the stack rather than on the heap. That often means that local variables have no existence on the stack.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siddharth Bhargava wrote:I thought that I have understood the String Pool concepts in Java completely and was confident about it till I came across this program below:

The fact is that the program you showed us looks like something that might have been given in an exam or interview question to find out how "advanced" a candidate is.

It is NOT an example of good programming; and furthermore, there is almost never a good reason to use
  new String(whatever)
these days. Use
  String.valueOf(whatever)
instead.

HIH

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!