• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interning

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All literals and constants are interned, so this:

public static final String HELLO = "Hello".intern();

is does nothing at all right? Harmless?

(editing - forgot the String!)
[ February 22, 2007: Message edited by: Stan James ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not the same as

public static final HELLO = "Hello";

This line of code allows the Java compiler to physically insert the constant in other classes that refer to it. (As you know this can yield confusing results if you change the value to be some other constant without recompiling all other classes that used it.) But your example doesn't have a constant value, so the compiler can't hardcode it into other classes.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Showing my ignorance, what is the "constant" quality that my example didn't have?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be "constant" in the sense that it doesn't change after initialization, but it's not a compile-time constant because of the method call. Basically if the compiler sees any method call, it figures that will have to be evaluated at run time.

Also "all literals and constants are interned" only seems to make sense to me if you're talking about Strings - is that the case?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that's news to me and good to know. Yes, this is about Strings only. I edited the original post to put String in where I forgot it the first time.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK, only literals are interned. I was not aware that constants would be interned automatically.



I don't think that is interned!?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mr. C Lamont Gilbert:
AFAIK, only literals are interned. I was not aware that constants would be interned automatically.



I don't think that is interned!?



No, it won't, but it is not a constant. Yes, all constants are interned. It is important to point out that in previous posts, the term 'compiler' refers to javac and not the runtime compiler, which may well (and often does) make the optimisations that are being asked about. It does this by reasoning that the type is 'immutable' (has only referentially transparent operations). In the case of String, the reasoning is shortcut by the simple fact that String is a compiler primitive.
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic