Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Autoboxing and ==

 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
In java 1.5 there is one rule as bellow -

This works only for range of values -128 to 127
This rule works for Boolean and Character,Short,Byte,Integer.
I have 1.6 and the same rule works for Long. Does this feature is available for Long in 1.5?
 
Sharmila Punde
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, i can figure out that by compiling with javac -source 1.5.
Sorry for trouble
Regards
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharmila Punde wrote:Dear All,
In java 1.5 there is one rule as bellow -


Are you sure?
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you probably have a typo, as I don't think 5 will equal 6, with any version of a Java compiler -- or probably with any compiler for any language.

Sharmila Punde wrote:Sorry, i can figure out that by compiling with javac -source 1.5.
Sorry for trouble
Regards


Well, you can check with Long -- but that won't confirm it. Basically, the Java specification only defines the range where the cache must be used. It does not state that for values outside the range, or for data types not specified, that a cache must not be used. This means that it is perfectly valid for an implementation of a JVM to cache values of a larger range, or of datatypes not specified.

I have 1.6 and the same rule works for Long. Does this feature is available for Long in 1.5?


There is no rule for Long. The Java specification does not require that the cache be applied for long values via autoboxing. It just happens that the Sun implementation caches those values for long -- but that doesn't mean that the specification requires it.

I would highly recommend that you do not write code that assumes (or depends) on this "feature".

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic