Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Just two literals

 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This code


shows that two literals are... actually only one literal. Is it guaranteed by JLS and will be correct on any JVM?

Thanks.
 
Steve Simon Joseph Fernandez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It should be guaranteed if the code that you wrote should provide the same output across JVM implementations

Anyways, given the immutable property of literals, it would be foolish to create two objects for the same literal.

_steve.
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by George Bolyuba:
Is it guaranteed by JLS and will be correct on any JVM?


You will need to dig into JLS for that.
But this can be explained by 'String pooling'. See
intern method in String class
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sonny Gill:

But this can be explained by 'String pooling'.


Yeah, I know about 'String pooling' and I explain behavior of my code just like you suggest.

Originally posted by Sonny Gill:

You will need to dig into JLS for that.

I hoped that someone did it for me . The background of my question was: "is the 'String pooling' mandatory for all JVM?". So, I believe I have to dig into JVM specification...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The behavior of your program is guaranteed.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The behavior of your program is guaranteed.


Ernest, your word is enough for me.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!