- class SCJPPrepration1 {
- public static void main(String[] args) {
- Integer i = Integer.valueOf(10);
- Integer j = Integer.valueOf(10);
- System.out.println(i==j);
- int magicSunOfficialSecretNumber =143;
- Integer k = Integer.valueOf(magicSunOfficialSecretNumber);
- Integer l = Integer.valueOf(magicSunOfficialSecretNumber);
- System.out.println(k==l);
- }
- }
Important thing that need to remember:
Whenever boxing is applied , only one wrapper object exists in the program for primitive values of( boolean,byte,char and int or short):
1).boolean values true or false,
2).a byte,
3).a char in range \u0000 to \u007f,
4),and an int or short value in the range -128 and 127.
so for eg if a and b refer to two wrapper objects that box same value which is among one of those mentioned above ,then a==b is always true,i.e object and reference equality will give same result..
Let us take an example,
but in case if we violate one of the above 4 points ,for eg here let us take point 4,
and do
Thus this proves the result.
1).boolean values true or false,
2).a byte,
3).a char in range \u0000 to \u007f,
4),and an int or short value in the range -128 and 127.
so for eg if a and b refer to two wrapper objects that box same value which is among one of those mentioned above ,then a==b is always true,i.e object and reference equality will give same result..
Let us take an example,
- Byte b1=(byte)10;
- Byte b2=(byte)10;
- System.out.println(b1==b2); //true
- System.out.println(b1.equals(b2)); //true
but in case if we violate one of the above 4 points ,for eg here let us take point 4,
and do
- Integer k=143;
- Integer l=143;
- System.out.println(k==l); //false because after boxing the range of int is bet[-128,127]
- System.out.println(k.equals(l)); //true as objects have same value
Thus this proves the result.