From primitive type to it corresponse wrapper class is called autoboxing, the reverse process is called unboxing. Autoboxing and unboxing also apply to methods calls. For example, you can pass an argument of type int to a method that has a formal parameter of type Integer.
A NullpointerException exception occurs When unboxing an null wrapper class's reference to its primitive type. For example, the code will compile but it will throw a NullpointerException at runtime.
...
Long L = null;
long l = L;
...
Boxing conversion converts values of primitive type to corresponding values of reference type. But the primitive types can not be widened/Narrowed to the Wrapper classes and vice versa. For example,
byte b = 43;
Integer I1 = 23; //Constant integer value
Integer I2 = (int)b; //Cast to int type
Long L1 = 23; //compile error because 23 is integer value
Long L2 = (Long)23; //can not cast integer value to Long wrapper class
Long L3 = 23L;
Long L4 = (long)23;
This restriction is also applied to method invocation:
public class MyClass {
public void method(Long i) {
System.out.println("Here");
}
public static void main(String[] args) {
MyClass s = new MyClasslass();
//s.method(12); // error
s.method(12L); // ok
}
}
When invoking a method from multiple overloading methods, For the matching method process, the Java compiler will perferance the order of primitive types.
Autoboxing is guaranteed to return the same object for integral values in the range [-128, 127], but an implementation may, at its discretion, cache values outside of that range. It would be bad style to rely on this caching in your code. Also, The Java Language Specification does not explicitly guarantee this behavior for long values within the range of a byte.
Creating a wrapper object by using keyword new will return different reference. For example,
class Program {
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = new Integer(20);
Integer i3 = new Integer(20);
System.out.println(i1 == i2);
System.out.println(i1 == i3);
}
}
The output is
false
false
class Program {
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = 20;
Integer i3 = 201;
Integer i4 = 201;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}
The output is
true
false
For small integral values, the objects are cached in a pool much like String pool. When i1 and i2 have the value of 20, they both point to a single object in the cache pool. While i3 and i4 have the value of 201, two separate objects are referenced.
A NullpointerException exception occurs When unboxing an null wrapper class's reference to its primitive type. For example, the code will compile but it will throw a NullpointerException at runtime.
...
Long L = null;
long l = L;
...
Boxing conversion converts values of primitive type to corresponding values of reference type. But the primitive types can not be widened/Narrowed to the Wrapper classes and vice versa. For example,
byte b = 43;
Integer I1 = 23; //Constant integer value
Integer I2 = (int)b; //Cast to int type
Long L1 = 23; //compile error because 23 is integer value
Long L2 = (Long)23; //can not cast integer value to Long wrapper class
Long L3 = 23L;
Long L4 = (long)23;
This restriction is also applied to method invocation:
public class MyClass {
public void method(Long i) {
System.out.println("Here");
}
public static void main(String[] args) {
MyClass s = new MyClasslass();
//s.method(12); // error
s.method(12L); // ok
}
}
When invoking a method from multiple overloading methods, For the matching method process, the Java compiler will perferance the order of primitive types.
Autoboxing is guaranteed to return the same object for integral values in the range [-128, 127], but an implementation may, at its discretion, cache values outside of that range. It would be bad style to rely on this caching in your code. Also, The Java Language Specification does not explicitly guarantee this behavior for long values within the range of a byte.
Creating a wrapper object by using keyword new will return different reference. For example,
class Program {
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = new Integer(20);
Integer i3 = new Integer(20);
System.out.println(i1 == i2);
System.out.println(i1 == i3);
}
}
The output is
false
false
class Program {
public static void main(String[] args) {
Integer i1 = 20;
Integer i2 = 20;
Integer i3 = 201;
Integer i4 = 201;
System.out.println(i1 == i2);
System.out.println(i3 == i4);
}
}
The output is
true
false
For small integral values, the objects are cached in a pool much like String pool. When i1 and i2 have the value of 20, they both point to a single object in the cache pool. While i3 and i4 have the value of 201, two separate objects are referenced.
No comments:
Post a Comment