reddy
reddy

Reputation: 675

autoboxing not working in java

I have the following Java class:

public class HelloWorld{

    public static void main(String []args){

        String s = 23.toString();//compilation error is ';' expected
        s = s + "raju";
        System.out.println(s);
    }
}

but as per auto boxing 23.toString() must convert to new Integer(23).toString() and executes that line. So why am I still getting a compilation error?

Upvotes: 2

Views: 1554

Answers (6)

Michael Kazarian
Michael Kazarian

Reputation: 4462

You wrote about type type conversion, not autoboxing.

For convert to string you can do next:

String s="23";

or

Integer i = new Integer(23);
s=i.toString()+"raju";

Autoboxing is automatical convert primitive int to Integer:

Integer i = 23; //Old variant Integer i = new Integer(23);
int a = i; //Old variant int i = (int) someInteger;

Upvotes: 1

Bob Cross
Bob Cross

Reputation: 22292

You're confused about when autoboxing is expected to work. In this case, you're trying to use an Object method on a plain old data type (an int).

Instead, try Integer.valueof():

public class HelloWorld{

    public static void main(String []args){
        // String s = 23.toString() will not work since a int POD type does not
        // have a toString() method.
        // Integer.valueOf(23) gets us an Integer Object.  Now we can 
        // call its toString method
        String s=Integer.valueof(23).toString();
        s=s+"raju";
        System.out.println(s);
    }
}

Autoboxing would work if you were passing that int to a method that expected an Integer as a parameter. For example:

List<Integer> intList = new ArrayList<>();
// Autoboxing will silently create an Integer here and add it to the list
intList.add(23);
// In this example, you've done the work that autoboxing would do for you
intList.add(Integer.valueof(24));
// At this point, the list has two Integers, 
// one equivalent to 23, one equivalent to 24.

Upvotes: 5

JamesB
JamesB

Reputation: 7894

23 is of type int, not Integer. It is a primitive, not an object.

Integer.valueOf(23).toString();

This is better than using the constructor as the valueOf method will use cache values in the range -128 to 127.

You probably want to refer to this: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

Upvotes: 5

Benten
Benten

Reputation: 1039

It dosen't work because Java will not let you dereference a primitive. Autoboxing works for things like assignment and passing a primitive to a method in place of an object.

Upvotes: 0

Burkhard
Burkhard

Reputation: 14738

It is not autoboxing what you are doing. Take a look here.

This should work:

public class HelloWorld{

    public static void main(String []args){
        Integer i=23;//autoboxing int to Integer
        String s=i.toString();
        System.out.println(s);
    }
}

Upvotes: 1

Areo
Areo

Reputation: 938

23 is int primitive, replace with new Integer(23) (wrapper on primitive)

Upvotes: 3

Related Questions