Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

String concepts in Java

 
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 ....
 
Marshal
Posts: 25682
69
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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: 954
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: 12165
258
  • 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.
 
Rancher
Posts: 4625
47
  • 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: 954
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
Rancher
Posts: 4625
47
  • 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: 12165
258
  • 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: 12165
258
  • 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
Rancher
Posts: 4625
47
  • 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: 954
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I never knew that..
 
Marshal
Posts: 69894
278
  • 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: 10777
71
Hibernate Eclipse IDE 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
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic