aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chew on this! 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 "Chew on this!" Watch "Chew on this!" New topic
Author

Chew on this!

Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
What are the results of the following code?
Caution: It may "byte" you if you're not careful!

Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
No takers? How about if I give you the results:
true true
false true
true false

Given:
Byte B1 = 5;
Byte B2 = 5;
Byte B3 = new Byte((byte)5);


Why are the following false?
B1==B3
B1.equals(5)
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Byte B1 = 5;
Byte B2 = 5;
Byte B3 = new Byte((byte)5);
B1==B2 will give you true always within all of their range.Conceptually it should be different objects but is being modified for their(bytes) whole range.The same is not the case for B1 and B3.
B1.equals(5).To understand this please have a look on the overriden equals() in wrapper class Byte.

when one calls B1.equals(5),the primitive 5 is first Boxed into Interger wrapper and then if(obj instanceof Byte) check fails,finally the method returns false.Changing method call to B1.equals((byte)5).In this case 5 will be boxed to Byte wrapper and hence will pass if(obj instanceof Byte) finally returns true.


~Sanjeev Singh<br />SCJP 1.5
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
Excellent Sanjeev

The situation for Bytes is analagous to that of Strings (I think). For Bytes there is a cache of values that is returned when autoboxing is used. (See K&B p. 236.)

Actually it appears from the source code that anytime the valueOf() method is called the Byte cache is used. (I assume that autoboxing is using valueOf() implicitly.)



This is not true when a constructor is called. Here it apparently creates a new Byte object and assigns a byte value to the instance variable "value". Oddly enough the constructor doesn't seem to look into the Byte cache to see if there is already an existing Byte with this same value.



There is a comment above in the valueOf() code about the performance implications. So really most of the time the valueOf() method shoud be used to create a Byte and not the constructor! The valueOf() method works as a sort of "getInstance of Byte withe this value from the byte cache". If you really want a unique instance of Byte (for some reason) with the assigned value then you can use the constructor! It's up to you.

Isn't Java grand!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Anytime the new operator is used, a new object is created.
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
I have a doubt in last statement



B1==5 prints true.

Now here there are two possibilities:

1. B1 gets unboxed to byte and gets compared to 5.
2. 5 gets auto-boxed to Byte and references gets compared.

Cna any one please tell which of the two options is correct?


SCJP 5.0 77%
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
Good question Satish! I hadn't thought of that. My guess is that option 1 is correct (B1 gets unboxed). I just think that if 5 gets auto-boxed, then it will probably go for an Integer instead of a Byte. I don't know the answer.

Anyone have the answer?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the Java Language Specification 15.21.1.

If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (�5.1.8) to numeric type, binary numeric promotion is performed on the operands (�5.6.2).

This is from 5.6.2.

When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (�5.1.2) to convert operands as necessary:

* If any of the operands is of a reference type, unboxing conversion (�5.1.8) is performed. Then:
* If either operand is of type double, the other is converted to double.
* Otherwise, if either operand is of type float, the other is converted to float.
* Otherwise, if either operand is of type long, the other is converted to long.
* Otherwise, both operands are converted to type int.
Franz Fountain
Ranch Hand

Joined: Nov 15, 2006
Posts: 58
So the answer is?

I think it's B1 gets unboxed to a byte and then promoted to a int for the comparison with the int literal 5.

Am I right?
[ November 21, 2006: Message edited by: Franz Fountain ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
That's right. That's what it's saying.

If one of the operands of == is a numeric primitive, and the other can be converted to a numeric primitive, then the other is converted to a numeric and both operands are converted under the rules of binary numeric promotion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chew on this!
 
Similar Threads
Boolean
Wrappers question
doubt in java conversion
Wrappers -
byte 0x7f, 0x80, 0x81