int is a primitive data type . It is a 32-bit signed two's complement integer which can have a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). Integer is a wrapper class for int. The constuctor for Integer class is Integer(int num / String str) . So Integer class wraps a primitive type int value in an object. An object of Integer class contains a single field whose type is int .
An int stores a number where as Integer points a reference of an object that contains a number .Integer class is an example of immutable object which represents a single value that cannot change over the lifetime of the object.
We may use Integer rather than a primitive due to some of the below mentioned reasons.
1. Some Methods of a class (for example methods of collection class like List , Map, etc can hold object references) accept only object reference (not primitive value) as an argument . 2. Integer class has many methods & constants which can be used in many ways . For example Constants of the Integer class, such as MIN_VALUE & MAX_VALUE, SIZE which is used to define the lower , upper value & number of bits to represent an int value and Methods of Integer Class su as parseInt , toString() are useful when dealing with an int.
Converting an Integer to int vice versa is very easy .
To convert to Integer from int. // box
int number =100;
Integer numWrap=new Integer(number);
//To convert to int from Integer // unbox
Integer numWrap1=new Integer(200);
int num =numWrap1.intValue();
If you use java 1.4 , then you have to manually box , unbox using the above commands.
The following example code will be compiled with java version 1.5 and above .No need to box manually. In Java 1.5 , new feauture autoboxing , unbox is introduced .
class intVsInteger
{
public static void main(String[] args)
{
int num1 = 100;
int num2 = 100;
int num3 = 160;
int num4 = 160;
Integer num5=300; // by autoboxing available from jdk 1.5
Integer num6=300; // by autoboxing
int num7=num5+num6; // by autoboxing
System.out.println( "num1= "+num1 );
System.out.println("num2= "+num2 );
System.out.println("num1==num2 " + ( num1==num2) );
System.out.println( "num3= "+num3 );
System.out.println("num4= "+num4 );
System.out.println("num3==num4 " + (num3==num4) );
System.out.println( "num5= "+num5 );
System.out.println("num6= "+num6 );
System.out.println("num5==num6 " + (num5==num6) );
System.out.println("Below value using Integer class");
intVsInteger(num1,num2);
intVsInteger(num3,num4);
intVsInteger(num5,num6);
}
public static void intVsInteger( Integer a , Integer b ) // using autoboxing
{
System.out.println( "a= "+a );
System.out.println("b= "+b );
Integer c=a+b;
int d=a+b; // using unbox
System.out.println("c= " + c );
System.out.println(a==b );
}
}
Output of the program
D:\as2\JF2>java intVsInteger
num1= 100
num2= 100
num1==num2 true
num3= 160
num4= 160
num3==num4 true
num5= 300
num6= 300
num5==num6 false
Below value using Integer class
a= 100
b= 100
c= 200
true
a= 160
b= 160
c= 320
false
a= 300
b= 300
c= 600
false
The intVsInteger method accepts Integer object . So the object reference is passed to the method not the value. if both are pointing to the same reference of the object , then the output will be true otherwise the output will be false.When using primitive type , if both values are equal means , num1=num2 will be true.
0 comments:
Post a Comment