Reading
Add Comment
Each primitive type (listed in Appendix ) has a corresponding type-wrapper class (in package java.lang). These classes are called Boolean, Byte, Character, Double, Float, Integer, Long and Short. These enable you to manipulate primitive-type values as objects.
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
Link
Or
read
AutoBoxing and unboxing
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes
1.Link
2.Link
3.Link
Consider the following example:
In line 1, the primitive values 1,2, and 3 are automatically boxed into objects new Integer(1), new Integer(2), and new Integer(3). In line 2, the objects intArray[0],intArray[1], and intArray[2] are automatically unboxed into int values that are added together
There are three reasons that you might use a Number object rather than a primitive:
As an argument of a method that expects an object (often used when manipulating collections of numbers).
To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Link
Uses:
1.As java generic Cannot Instantiate Generic Types with Primitive Types thus we use their wrapper class as argument type.
important
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int.
Link
Or
read
AutoBoxing and unboxing
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes
1.Link
2.Link
3.Link
Consider the following example:
1: Integer[] intArray = {1, 2, 3};
2: System.out.println(intArray[0] + intArray[1] + intArray[2]);
In line 1, the primitive values 1,2, and 3 are automatically boxed into objects new Integer(1), new Integer(2), and new Integer(3). In line 2, the objects intArray[0],intArray[1], and intArray[2] are automatically unboxed into int values that are added together
There are three reasons that you might use a Number object rather than a primitive:
As an argument of a method that expects an object (often used when manipulating collections of numbers).
To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).
Link
Uses:
1.As java generic Cannot Instantiate Generic Types with Primitive Types thus we use their wrapper class as argument type.
public class marc4<T> {
private T t;
public T get (){
return t;
}
public void add(T t){
this.t=t;
}
public static void main (String args[]){
marc4<Integer> sInt=new marc4<Integer>();
sInt.add(new Integer(12));
//sInt.add(4); ///AutoBoxing is happing here compiler covert premitive //into wrapper class
System.out.println(sInt.get());
}
}
0 comments:
Post a Comment