hi all , funny question : Byte b = new Byte(123);//fails to compile! Compiler says : Incompatible type for constructor. Explicit cast needed to convert int to byte. here: Byte b = new Byte((byte)123); //compiles. byte b1 = 123; //compiles just fine too. Java api doc constructor for Byte class is: public Byte(byte value) & 123 is a perfect value for a byte since byte range is (-128 : 127). Even when i use second constructor of Byte class: Byte(String value) - Byte bb = new Byte("123"); //it fails to eat too ! So why do i need to cast number 123 to a byte if it's a legal byte type ? Thank u, Ivan
Don't forget that *all* integral literals are of type int. when you write Byte b = new Byte(123);//fails to compile the compiler interprets the literal "123" as an int. It tries to call a contructor for Byte that takes an int argument. Since there isn't one, you get an error. Casting the int literal "123" to a byte results in a byte value that can be passed to the method. Byte b = new Byte((byte)123); This is the same issue as doing this: byte b = 12; byte c = b + 1 ; //compiler error!! The expression (b + 1) is of type int, because of the rules governing numeric promotion of binary operators. When you try to assign this int result back into a byte variable, the compiler tells you that you can't do that. byte c = (byte)(b+1) ; //works fine. Rob
byte b = 12; what type will the compiler interprets number 12 as? byte or int? if it is int,why didn't need to cast?
help you means help me
Joined: Jan 07, 2002
This is a special exception for literals. Since the compiler can verify that the assignment won't loose any information, it does an implicit narrowing conversion for you. It's like the compiler writes byte b = (byte) 12; for you. This implicit narrowing conversion works for byte, char, and short. If the value you are assigning would loose information, you'd get a compiler error: byte c = 130; //compiler error, 130 not valid number for a byte.
Hello Rob, I am still not clear with this. If it is byte b = 12 //It will compile right? and if it is Byte b = 12//It will not compile why?? Can any one clear this with a detailed explaination? Sonir
Joined: Nov 01, 2001
Hello... Rob, Ivan are you'll there.. Need your comments!! Sonir
Joined: Jan 07, 2002
Originally posted by sonir shah: Hello Rob, I am still not clear with this. If it is byte b = 12 //It will compile right? and if it is Byte b = 12//It will not compile why?? Can any one clear this with a detailed explaination? Sonir
For byte b = 12; you can compile it yourself and see whether it works or not! as for Byte b = 12; you can also compile it and see what happens. Just remember though that Byte is a class , and byte is a primitive type (note the uppercase letter 'B' in Byte). Since it's a class type, you must instantiate a new Byte object with the 'new' operator. Rob
Ivan says: Even when i use second constructor of Byte class: Byte(String value) - Byte bb = new Byte("123"); //it fails to eat too ! Ivan, try again, you should not have problem compiling Byte bb=Byte("123") at all. Even if the string you give evaluates to something greater than 127, in which case, you will have runtime exceptions. [ January 18, 2002: Message edited by: Gene Chao ]
byte b = 123; Byte v = new Byte(b); // work fine It works, as said by Rob, compiler has already gave green signal to b to proceed the value it contain by verifying the literal value and scope of the byte. Thus we can give b as an argument to Byte constructor which contain byte value not integer.