In older versions of java , int (or any other primitive value) are not accepted by a collection. Consider the following code for converting array to ArrayList which gives the compile error due to Collection accepts object references not primitive int values. The solution for this error is to box primitive values into the appropriate wrapper class . In this case we have to convert int to Integer wrapper class . Now replace the line lst.add(numbers[i]); with lst.add(new Integer(numbers[i])); here we have converted int to Integer wrapper class.
import java.util.*;
class listExample
{
public static void main(String args[])
{
List lst = new ArrayList();
int numbers[] ={10,30, 55, 65,33};
for (int i=0;i<5;i++)
{
lst.add(numbers[i]); // Error listExample.java:13: cannot resolve symbol , symbol : method add (int) , location: interface java.util.List . Solution : Replace lst.add(numbers[i]); with lst.add(new Integer(numbers[i]));
System.out.println(numbers[i]);
}
}
}
When we need an int, we have to unbox the Integer using the intValue method . Consider the following lines of code , where add method accepts two Object references (Not a primitive value) , when we compile the code in older version of java , it throws error , because we are adding two objects reference and assigning to a primitive int variable. . Replacing the int d=a+b; with int d=a.intValue()+b.intValue(); will solve the problem.
class Addition
{
public static void main(String args[])
{
Integer k=new Integer(123);
Integer k1=new Integer(1523);
add (k, k1);
}
public static void add( Integer a , Integer b )
{
int d=a+b; // using unbox
System.out.println("Addition = " + d );
}
}
The following example illustrates autoboxing and unboxing . In the main function , we have declared 4 variables a, b , c, d . a & b are primitive type variables. c & d are Integer class variables. Now in our code , we have assigned primitive type int value to Integer c & Integer d . In older version , the this line gives compile error. But in java 1.5 , it does not give any error due to autoboxing . The autoBoxUnbox( Integer a , Integer b ) accepts two object reference. So when we call autoBoxUnbox(a,b) where a , b are int values , in java 1.5 no error is coming due to autoboxing. In the autoBoxUnbox method , the line int d=a+b; d is an int variable. but a & b are Integer variable. Here a & b are converted to int variable by unboxing in java1.5 , In java 1.4 we have to use Integer.intValue() to convert Integer to int.
import java.util.*;
class autoBoxUnbox
{
public static void main (String args[])
{
int a = 100;
int b = 100;
Integer c=300;
Integer d=300;
autoBoxUnbox(a,b);
autoBoxUnbox(c,d);
}
public static void autoBoxUnbox( Integer a , Integer b ) // using autoboxing
{
Integer c=a+b;
int d=a+b; // using unboxing
System.out.println("Given Numbers " + a + " , " + b);
System.out.println("Addition = " + c );
System.out.println("Addition using unbox= " + d );
System.out.println(" ");
}
}
Output :
D:\as2\JF2>java autoBoxUnbox
Given Numbers 100 , 100
Addition = 200
Addition using unbox= 200
Given Numbers 300 , 300
Addition = 600
Addition using unbox= 600
0 comments:
Post a Comment