wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Autoboxing and == Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Autoboxing and ==" Watch "Autoboxing and ==" New topic
Author

Autoboxing and ==

Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
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?


Regards, Sharmila.
We will have a future if we save Nature!
Sharmila Punde
Ranch Hand

Joined: Dec 26, 2007
Posts: 133
Sorry, i can figure out that by compiling with javac -source 1.5.
Sorry for trouble
Regards
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Sharmila Punde wrote:Dear All,
In java 1.5 there is one rule as bellow -


Are you sure?

http://muhammadkhojaye.blogspot.com/
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19065
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing and ==