Results 1 to 6 of 6

Thread: Autoboxing

  1. #1
    p0o0o0ya is offline Member
    Join Date
    Jan 2011
    Location
    Iran-Zanjan
    Posts
    4
    Rep Power
    0

    Default Autoboxing

    hi guys.
    what' s up?
    I wrote this program:
    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); 
    	}
    }//~
    and compiler give me this error:
    ***
    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.

  2. #2
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    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!!

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    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

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by p0o0o0ya View Post
    Java Code:
    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
    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 07:01 AM.

  5. #5
    baloda is offline Member
    Join Date
    Nov 2010
    Location
    New Delhi
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by mine0926 View Post
    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
    'Float' is also valid Floating point data type check out java.lang.Float. and u r also ignoring error msg.

    @p0o0o0ya go with as i mentioned.
    Last edited by baloda; 02-03-2011 at 07:04 AM.

  6. #6
    p0o0o0ya is offline Member
    Join Date
    Jan 2011
    Location
    Iran-Zanjan
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by mine0926 View Post
    First java is case sensitive so it should be
    long not Long
    and
    float not Float
    [/code]
    ok. I know that java is case sensitive but,
    long is a primitive
    and Long is a wrapper type.

Similar Threads

  1. autoboxing in action
    By Ahmed.Java in forum Java Software
    Replies: 1
    Last Post: 04-29-2010, 01:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •