Win a copy of The Java Performance Companion this week in the Performance 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
Marshal
Pie
Posts: 24211
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic