aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Byte b = new Byte(123); //fails to compile ! :mad: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Byte b = new Byte(123); //fails to compile ! :mad: " Watch "Byte b = new Byte(123); //fails to compile ! :mad: " New topic
Author

Byte b = new Byte(123); //fails to compile ! :mad:

Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
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
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
I see now ! THANK U !
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
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
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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.

Rob
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
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
sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Hello...
Rob, Ivan are you'll there..
Need your comments!!
Sonir
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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
Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
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 ]
shiren shah
Greenhorn

Joined: Nov 16, 2001
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte b = new Byte(123); //fails to compile ! :mad: