byte to int casting question

Y

yair

hey
I wrote this piece of code:

1 : import java.util.Vector;
2 :
3 : public class Try1 {
4 : public static void main(String args[]) {
5 : Vector vec = new Vector();
6 : vec.add(new Byte((byte)27));
7 : byte b = ((Byte) vec.elementAt(0)).byteValue() + (byte)3;
8 : System.out.println(b);
9 : }
10: }

javac will not compile this code, because it sais:
If i'll cast to byte it will be compiled:
byte b = (byte) (((Byte) vec.elementAt(0)).byteValue() + (byte)3);

y isn't it working in the original form? I dont c any hidden casting
from byte to int.

thanks
yair
 
L

Lothar Kimmeringer

byte b = (byte) (((Byte) vec.elementAt(0)).byteValue() + (byte)3);

y isn't it working in the original form? I dont c any hidden casting
from byte to int.

The addition "returns" int.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
T

Tor Iver Wilhelmsen

y isn't it working in the original form? I dont c any hidden casting
from byte to int.

Yes you do, in the form of the + operator. All integer mathematical
operations occur in int "space" unless one of the operans is a long.
 
G

Gary Labowitz

yair said:
y isn't it working in the original form? I dont c any hidden casting
from byte to int.

Shouldn't this be:
y isn't workin in orig frm? I dont c ne hidn castin frm b to i
??
 
R

Roedy Green

y isn't workin in orig frm? I dont c ne hidn castin frm b to i

Please write in English. Any time you do an addition the operands
will be promoted to int or long or float or double as appropriate.
There is no char or byte addition.
 
Y

yair

Roedy Green said:
Please write in English. Any time you do an addition the operands
will be promoted to int or long or float or double as appropriate.
There is no char or byte addition.

i thought that maybe the "+" is done as ints, but this line DOES compile:

byte b = ((byte)4) + ((byte)3);

and here u have byte=byte+byte.
y?
 
R

Roedy Green

i thought that maybe the "+" is done as ints, but this line DOES compile:

byte b = ((byte)4) + ((byte)3);

and here u have byte=byte+byte.

if it does accept it, it is because you have literals. With byte
variables, it will complain.
 
V

VisionSet

yair said:
Please write in English. Any time you do an addition the operands
will be promoted to int or long or float or double as appropriate.
There is no char or byte addition.

i thought that maybe the "+" is done as ints, but this line DOES compile:

byte b = ((byte)4) + ((byte)3);

and here u have byte=byte+byte.[/QUOTE]

The int 4 is cast to a byte, the int 3 is cast to a byte, the presence of
the + operator causes both operands to then be implicitly cast to an int,
this happens with all primitives narrower than an int, literal or not. The
ints are summed to give an int of 7.
The 7 fits into a byte with no loss of precision so byte b is initialised to
7.
This is all done at compile time since they are literals, so it can be
determined whether there is a loss in precision or not.
Trying to add two bytes as bytes is impossible there will always be an
implicit conversion prior to the operation.
You can of course cast afterwards which is what you would have to do if they
were variables. ie

byte b1 = 4, b2 = 3;
byte b3 = (byte)(b1 + b2);
 
C

Chris Smith

yair said:
i thought that maybe the "+" is done as ints, but this line DOES compile:

byte b = ((byte)4) + ((byte)3);

and here u have byte=byte+byte.

The other two replies you see are basically right. Here's some more
detail:

Indeed, it's true, per sections 15.18.2 (on the + and - numeric
operators) and 5.6.2 ("Binary Numeric Promotion") of the Java Language
Specification, that the result of the right-hand side of the assignment
above is of type 'int'. However, it is also a "constant expression" (as
defined in section 15.28). Because it's a constant expression being
assigned to a variable of type 'byte', section 5.2 ("Assignment
Conversion") allows for a narrowing primitive conversion to be implied
by the assignment, but only if the actual value of the expression fits
into the target data type. In this case, it does.

The result is that all of the following are valid:

byte b = 5; (5 is a constant expression)

byte b = 5 + 7; (so is '5 + 7')

final int i = 7;
byte b = 5 + i; (and even '5 + i', because i is a final
variable whose initializer is a constant
expression (per 15.28))

The following, though are not valid.

byte b = 128; (constant expression, but out of range)

float f = 12.0; (constant and in range, but target type must be
byte, short, or char)

byte b = 127 + 1; (again, out of range)

int i = 7;
byte b = 5 + i; (not a constant expression, because
i is not final)

int a = 7;
final int i = a;
byte b = 5 + i; (i is final, but it's not not a constant
expression because its initializer is not
a constant expression.)

And so forth.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

Trying to add two bytes as bytes is impossible there will always be an
implicit conversion prior to the operation.

ironically though, their is a castless byte addition defined for b++.

This implies that even if the JVM has no byte add, the Java language
could have been defined so that the sum of two bytes is a byte.
However, this is not always what you want.

What is more of an annoyance in the lack of an octet type (unsigned
byte).
 
L

Lee Fesperman

Roedy said:
ironically though, their is a castless byte addition defined for b++.

This implies that even if the JVM has no byte add, the Java language
could have been defined so that the sum of two bytes is a byte.
However, this is not always what you want.

How could it be different?

b++ should increment b and have as a value the previous value of b.

++b should increment b and have as a value the new value of b.

It doesn't matter for b++. Perhaps, you meant ++b. Do you think:

x = ++b;

.... should be equivalent to:

x = b + 1;
b = (byte) x;

.... rather than:

b = (byte) b + 1;
x = b;

?
 
R

Roedy Green

How could it be different?

I did not mean to imply it should be different. I was pointing out
that the JVM is effectively capable of doing a byte addition. The
reason Java bytes don't add to form a byte is not because of some
limitation of the JVM.
 
L

Lee Fesperman

Roedy said:
I did not mean to imply it should be different. I was pointing out
that the JVM is effectively capable of doing a byte addition. The
reason Java bytes don't add to form a byte is not because of some
limitation of the JVM.

Makes sense to me. Sorry, didn't mean to come on too strong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top