Scenario
class Demo {
public static void main(String[] args) {
Integer i = 100;
Integer j = 100;
System.out.println(i==j);
Integer x = 127;
Integer y = 127;
System.out.println(x==y);
Integer a = 128;
Integer b = 128;
System.out.println(a==b);
Integer p = 100;
Integer q = new Integer(100);
System.out.println(p==q);
}
}
What value will be printed ?
Result
truetrue
false
false
Explanation
Wrappers are used from Object pool if value being boxed is :
- true or false
- A byte
- A char in the range \u0000 to \u007f
- An int or short number between -128 and 127 (inclusive)
Wrapper object created using new operator will always use separate memory in heap and will not pick pooled objects.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.