Results 1 to 6 of 6
Thread: Autoboxing
- 02-03-2011, 05:31 AM #1
Member
- Join Date
- Jan 2011
- Location
- Iran-Zanjan
- Posts
- 4
- Rep Power
- 0
Autoboxing
hi guys.
what' s up?
I wrote this program:
and compiler give me this error:Java Code://: object/E9 public class E9{ public static void main(String[ ] args){ Boolean Bo= true; boolean bo= Bo; System.out.println("Boolean = " + Bo); System.out.println("boolean = " + bo); Character Ch='a'; char ch=Ch; System.out.println("Character = " + Ch); System.out.println("char = " + ch); Byte By=1; byte by=By; System.out.println("Byte = " + By); System.out.println("byte = " + by); Short Sh=2; short sh=Sh; System.out.println("Short = " + Sh); System.out.println("short = " + sh); Integer I=3; int i=I; System.out.println("Integer = " + I); System.out.println("int = " + i); Long L=64; long l=L; System.out.println("Long = " + L); System.out.println("long = " + l); Float F=5.1; float f=F; System.out.println("Float = " +F); System.out.println("float = " + f); Double D=6.1; double d=D; System.out.println("Boolean = " +Bo); System.out.println("boolean = " + bo); } }//~
***
E9.java:24: incompatible types
found : int
required: java.lang.Long
Long L=64;
^
E9.java:28: incompatible types
found : double
required: java.lang.Float
Float F=5.1;
^
2 errors
****
what's wrong with my program?
thanks.
- 02-03-2011, 05:46 AM #2
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
initialize Long like
Long L=64L; in place of Long L=64;
initialize Float like
Float F=5.1F; in place of Float F=5.1; this is the right way.
good Luck!!
- 02-03-2011, 05:53 AM #3
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
First java is case sensitive so it should be
long not Long
and
float not Float
Second, declaring float's value should have f on its ending.
Java Code:float aFloatType = 12.25f
- 02-03-2011, 05:59 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
In both cases the compiler means what it says. You attempted to assign int and double values (the literals 64 and 5.1) but the compiler demands long and float values when assigning to Long and Float variables.
To elaborate a little, from the Java Language Specification...
Regarding integer literals: "An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int". And for floating point literals: "A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d."
Assignment always involves an assignment conversion. As indicated there, such conversions come in various flavours. Of interest here is the allowed possibility of a boxing conversion. The options are limited and enumerated: int->Long and double->Float are not mentioned.Last edited by pbrockway2; 02-03-2011 at 06:01 AM.
- 02-03-2011, 06:02 AM #5
Member
- Join Date
- Nov 2010
- Location
- New Delhi
- Posts
- 50
- Rep Power
- 0
- 02-03-2011, 06:04 AM #6
Member
- Join Date
- Jan 2011
- Location
- Iran-Zanjan
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
autoboxing in action
By Ahmed.Java in forum Java SoftwareReplies: 1Last Post: 04-29-2010, 12:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks