This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

String Reference Type

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are reference type in Java, I created a code snippet to validate the same behavior but It doesn't look like working.

Code Snippet:-



Output:-

java is not fun
Java is fun!



Expected Output:-

java is not fun
java is not fun

 
Rancher
Posts: 117
3
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this has to do with the fact that java stores the str1 literal ("java is fun") in the string pool.
You then set str2 to point to this literal.
Consequently you make the str1 variable point to another literal ("java is not fun"), but the str2 variable is still referring to the first literal.
 
Marshal
Posts: 65041
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't think the String pool has anything to do with why the two are different. It has to do with assignment, as BG said. You are assigning the two references in that order, so that is what they will contain. The assignment in line 5 is applied before that in line 6 and the two are independent.Why did you expect to have both references pointing to the same String in line 7?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable, you cannot change it.
Your str1 is pointing to one string literal (as Brecht said).
Your str2 is pointing to the same String object.
But when you assign new value to str1, new String object is created in string pool(because the existing object cannot be changed), and reference str1 is automatically "redirected" to point to that new object. You cannot change existing string
 
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vineet walia wrote:Strings are reference type in Java
. . .
Output:-

java is not fun
Java is fun!



Expected Output:-

java is not fun
java is not fun

if you create string literal it is stored in string literal pool, so again if you create the same literal object with different reference then no new object is created but instead it will internd to same object as str1 and str2 points to same object, then if change the object value and assgin str1 then it current object pointing that reference still str2 pointing to first object
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this you need to memory allocation of that string object and memory address then you will get idea why it is reference type.
 
Campbell Ritchie
Marshal
Posts: 65041
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote:. . . if you create string literal it is stored in string literal pool, so again if you create the same literal object . . .

But the OP isn't writing the same String literal twice. It doesn't matter whether you use the String pool or not.
What matters is that the assignment operator takes the value on its right and applies it to the variable on its left. I think OP is getting confused and thinks it takes the contents of the variable on its left and applies that to the expression on its right, but it doesn't.

Please don't simply quote the whole of a preceding post, but only the relevant parts. I have removed some of the quote.
 
Sheriff
Posts: 13553
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There seems to be a fundamental misunderstanding of how the String Pool is populated. String literals can already be identified at compile time so you can think of the String Pool as a cache of String objects that is populated when classes are loaded. The description "creates a string literal that is interned and then creates another" is not actually what happens. It's more like you have a lookup table whose entries are referenced directly wherever the code uses a string literal.

String objects that are created at runtime can be interned, otherwise, they are created on the heap and are treated like any other dynamically instantiated object.
 
Brecht Geeraerts
Rancher
Posts: 117
3
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Junilu, for the insight! It helped clarify the concept of the String pool.
 
Junilu Lacar
Sheriff
Posts: 13553
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vineet walia wrote:... but It doesn't look like working.
...
Expected Output:-


Going back to OP's original question, it's not that it isn't working; it's working the way it's supposed to work. You're just not having the right expectation.

It seems like you think that by doing this: str2 = str1 you think that whatever you do with str1 will affect str2 so that when you reassign str1 to a different value, str2 will somehow follow along, as if it were attached to str1. That's not how it works at all.

Here's what does happen:

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