• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Literal Strings  RSS feed

 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain me these lines. i am not able to understand these lines

1. Literal Strings within the same class in the same package represent reference to the same string object.

2. Literal strings within different classes in same package represents references to the same String Object.

3. Literal Strings within different classes in the different packages likewise represent references to the same String Object.

4. Strings computed by constant expression are computed at compile time and then treated as if they were literals.


if code will be explained that will be very helpful. Thanks a lot in advance.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you tell us what book or website these sentences are from? Some information seems to be missing.

I know that String literals are not the same as String objects. Literals can be interned and point to the same reference, but String objects can't do this For instance:



That's the most sense I can make of those statements. I hope it helps.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Can you tell us what book or website these sentences are from? Some information seems to be missing.


I am practicing enthuware exam for java 8.

I see following question to predict the output:

Question: What will be the output of running class Test?


I was able to predict correct output of line 4 and line 5 but not the other 3.
If possible please explain. Other than this code will help too.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what was the output? I am 99.999% sure you will not quote the correct output even after running the program
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be sure to show where all the line ends are in the output.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
output of line 1 is true
output of line 2 is true
output of line 3 is also true
output of line 4 is false
output of line 5 is true





 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that true or true ?
Your output is actually
true true true false true
As one line. You are not printing the 1st to 4th boolean results, because before you try printing them you are catenating the output with the single‑character String to give t‑r‑u‑e‑ . So the output from line 1 is actually "true " with an additional space. If you miss out a pair of () you can get all sorts of strange outputs, but I think you get all but the last showing false.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes output is true true true false true
I am asking whyhas output as true?

why it doesn't make another memory location in string pool ?
like ^ it does!
Similarly other line 1 and 2 gives output as true, as they are from other classes and other packages, doesn't they make different memory locations for there respective string pools?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Hel"+"lo" is two literals that the compiler same optimize to the literal "Hello". "Hel"+lo contains a variable that the compiler cannot optimize. We can see that lo = "lo" but the compiler can't.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:"Hel"+"lo" is two literals that the compiler same optimize to the literal "Hello".


In other words, the concatenation of two compile time constants, is another compile time constant.

Knute Snortum wrote: "Hel"+lo contains a variable that the compiler cannot optimize. We can see that lo = "lo" but the compiler can't.


This is because the lo variable is not a compile time constant. If it was a compile time constant, such as if you also declared the variable as final, then the result would also have been true.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Linkon Manwani wrote:
Similarly other line 1 and 2 gives output as true, as they are from other classes and other packages, doesn't they make different memory locations for there respective string pools?


As an implementation, you are likely correct in that they have different internal string references.... but they should all refer to the same string object, that is referred to by the string pool.

Henry
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
but they should all refer to the same string object, that is referred to by the string pool.

Henry

Thanks a lot the explanation.

It means during compilation of file, compile time constant strings literals are stored somewhere in internal structure most probably in heap and referenced to a particular address i.e. string literal object, no matter whether that literal is in another class, file or package as long as that file or package is imported to the current file which is getting compiled.

Is it correct? Please correct me if i misinterpreted anything
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a class is compiled, the literal values are stored in the compiled class file. It has been a while since I worked at that level, but presumably those literals are dynamically instanced as String objects as needed.

You might want to investigate a java mechanism for Strings called "interning", which allows multiple sources of a single string value to be optimized down to a single JVM-wide instance of class String.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a Map somewhere in the JVM which records the locations of all String literals and probably also interned Strings. You put a String literal into it, it tells you whether you already have that literal (in which case you reuse the original copy) or you put the String as its own K and V.

And I must apologise; there I was going on about line ends because I hadn't read the code properly and thought it was using println when it was actually using print Sorry.
 
Linkon Manwani
Ranch Hand
Posts: 47
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

And I must apologise; there I was going on about line ends because I hadn't read the code properly and thought it was using println when it was actually using print Sorry.

It's alright. No need for sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!