my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Float and Byte Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Float and Byte" Watch "Float and Byte" New topic
Author

Float and Byte

Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128

1 compiles without any error but 2 doesn't compile. Why? And Float and Byte do not have constructor with int argument, but only Byte says "cannot resolve symbol
symbol : constructor Byte (int)
location: class java.lang.Byte
Byte b = new Byte(123);"
Thanks in advance.


SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
I would assume that the int value passed to the Float constructor is being promoted to a float value (valid, legal, automatic widening conversion), but the compiler isn't doing an automatic narrowing conversion of the int value to a byte. Casting to a byte as in Byte b = new Byte((byte) 123); should work


Rich
SCJP 1.4
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
First: Yes I agree, but I would understad "Incompatible types" error, but here "cannot resolve symbol constructor Byte (int)" error. The Float as the Byte does not has constructor which receives int parameter.
Second: I passed in Byte constructor int value which is go in byte size.
[ April 09, 2004: Message edited by: Serghei Jelauc ]
[ April 09, 2004: Message edited by: Serghei Jelauc ]
Richard Quist
Ranch Hand

Joined: Feb 18, 2004
Posts: 96
Originally posted by Serghei Jelauc:
First: Yes I agree, but I would understad "Incompatible types" error, but here "cannot resolve symbol constructor Byte (int)" error. The Float as the Byte does not has constructor which receives int parameter.
Second: I passed in Byte constructor int value which is go in byte size.
[ April 09, 2004: Message edited by: Serghei Jelauc ]
[ April 09, 2004: Message edited by: Serghei Jelauc ]

For point 1: My understanding is that the compiler will do the widening conversion (from smaller type to a larger) for you automatically since there's no loss of precision...so in your case it would have casted the int literal to a float type...the JLS describes this: Widening conversion
For point 2: The compiler will, I believe, accept the assignment of the value 123 to a variable declared as a byte type because the value is small enough to fit into a byte, but will not do the automatic cast to the smaller type (from the int literal to a byte) in the method call...here's a link in the JLS about this: No Narrowing Conversion in Method calls
Hope that helps
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Float and Byte
 
Similar Threads
Wrapper Class Problem
wrappers
Byte Doubt
Get file and getBytes()
primitive widening doubt