Integer vs int in java
Is there anything interesting between them.
If you are a Junior Java developer then maybe it is fun.
If you are a Senior Java developer then maybe it is also funny for you
If you are a Profesional Java Developer then maybe it is not excited
Anyone try to run this block code
---> It is clearly: true
--> It is still true
-> It is still true ?????? No, It is false. If you dont belive You can run it online
https://www.jdoodle.com/online-java-compiler
So what happened ????
All you need to remember that
You have a Integer Box that contains {-128 ,-127, -126, ..., 0, ... , 125, 126, 127}
So when you use this command:
Integer x= 123;
Because -128 =< 123 <= 127
It will search in Integer Box and return the memory address of that value
Integer y = 123;
It return the same memory address of 123
So x == y return TRUE because x and y were the same memory address
-----------------------------------
If you code
Integer x= 128;
Integer y=128;
128 is not in Integer Box
Then x and y has different memory address. Then x == y will return false
----------------------------------
Integer x = new Integer(123); --> new heap memory address
Integer y =123; -> Integer Box memory address
Then x==y return false
----------------------------------
Integer x= 300;
int y =300;
Then x == y return true; Because x will be cast to int value, and they will compare the same value of 300
-------------------------------
Integer x = new Integer(100); a new heap address memory
Integer y = new Integer(100); another new heap address memory
then x == y return false. Because It compare the address in heap memory
If you are a Junior Java developer then maybe it is fun.
If you are a Senior Java developer then maybe it is also funny for you
If you are a Profesional Java Developer then maybe it is not excited
Anyone try to run this block code
public static void main(String[] args) {
Integer x = new Integer(1);
int y = 1;
if (x == y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
---> It is clearly: true
public static void main(String[] args) {
Integer x = 100;
Integer y = 100;
if (x == y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
--> It is still true
public static void main(String[] args) {
Integer x = 300;
Integer y = 300;
if (x == y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
-> It is still true ?????? No, It is false. If you dont belive You can run it online
https://www.jdoodle.com/online-java-compiler
So what happened ????
All you need to remember that
You have a Integer Box that contains {-128 ,-127, -126, ..., 0, ... , 125, 126, 127}
So when you use this command:
Integer x= 123;
Because -128 =< 123 <= 127
It will search in Integer Box and return the memory address of that value
Integer y = 123;
It return the same memory address of 123
So x == y return TRUE because x and y were the same memory address
-----------------------------------
If you code
Integer x= 128;
Integer y=128;
128 is not in Integer Box
Then x and y has different memory address. Then x == y will return false
----------------------------------
Integer x = new Integer(123); --> new heap memory address
Integer y =123; -> Integer Box memory address
Then x==y return false
----------------------------------
Integer x= 300;
int y =300;
Then x == y return true; Because x will be cast to int value, and they will compare the same value of 300
-------------------------------
Integer x = new Integer(100); a new heap address memory
Integer y = new Integer(100); another new heap address memory
then x == y return false. Because It compare the address in heap memory
Comments
Post a Comment