In java "==" is used to compare object references. The caching in Java allows to reuse the referenced objects and save memory. So in the given range of caching the same object reference is used and the comparison results as true while beyond the range it returns false.
Note :
1) Caching was introduced in Java 5
2) It basically improves the performance
3) Caching range lies between -127 to +127
4) Caching applies for Integer , Byte , short , Long and Character objects too
5) It only works for autoboxed type of objects. It is not applicable for normal object declaration(eg : Integer integer1 = new Integer(123);.
Example :
Output:
Note :
1) Caching was introduced in Java 5
2) It basically improves the performance
3) Caching range lies between -127 to +127
4) Caching applies for Integer , Byte , short , Long and Character objects too
5) It only works for autoboxed type of objects. It is not applicable for normal object declaration(eg : Integer integer1 = new Integer(123);.
Example :
public static void main(String[] args) {
Integer integer1 = 123;
Integer integer2 = 123;
Integer integer3 = 128;
Integer integer4 = 128;
if (integer1 == integer2)
System.out.println("integer1 == integer2");
else
System.out.println("integer1 != integer2");
if (integer3 == integer4)
System.out.println("integer3 == integer4");
else
System.out.println("integer3 != integer4");
}
Output:
integer1 == integer2 integer3 != integer4
No comments:
Post a Comment