• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interning  RSS feed

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Showing my ignorance, what is the "constant" quality that my example didn't have?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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!?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!