This question already has answers here:
Java: Integer equals vs. ==
(7 answers)
How can I properly compare two Integers in Java?
(10 answers)
Closed 2 years ago.
Integer int1 = 2;
Integer int2 = 2;
System.out.println(int1 == int2); // ok
Run code
/Libray/.../jdk1.8.0_241.jdk/.../..java...
true
Process finished with exit code 0
Compare Double object
Double dou1 = 4.0;
Double dou2 = 4.0;
System.out.println(dou1 == dou2);
Run code
/Libray/.../jdk1.8.0_241.jdk/.../..java...
false
Process finished with exit code 0
I think it's possible to compare integers using autoboxing and unboxing, but why it doesn't work for Double?
Related
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
((13 / 3 == 4) == true)
why is this equals true?
13/3 = 4.3333 and
4.333 is not equaled 4.
Is it about auto cast into an integer? and round?
I tested it in Java EE 8.
Because when you write 13 / 3 you have divided two integer, so the result is only int part, so 4.
In this way you have the next condition 4 == 4 is true
This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Why Comparison Of two Integer using == sometimes works and sometimes not? [duplicate]
(3 answers)
Weird Integer boxing in Java
(12 answers)
Closed 4 years ago.
Integer i3 = 10;
Integer i4 = 10;
System.out.println(i3 == i4);
Integer i5 = 1000;
Integer i6 = 1000;
System.out.println(i5 == i6);
I got output for the above code as,
true
false
I can understand how i3==i4, but what is the reason to i5==i6 become false.
This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Closed 5 years ago.
Can someone explain me the logic why a and b are both equal and x and y are not both equal but they have the same value.
Integer a = 40;`// integer a
Integer b =40; // integer b
Integer x = 400; // why x and y are not equal
Integer y = 400; // how is that possible
if (a==b){ //first condition
System.out.println("a=b"); //if true print
}else {
System.out.println("a!=b"); // if not true
}
if(x==y){ // second condition
System.out.println("x=y"); // if true print
}else{
System.out.println("x!y"); // if not true print
}
it is a mistake to compare objects (or really a bad practice) with the ==, for that most of the objects overwrite the compareTo() , and obvious the Intege is not the exception.
This question already has answers here:
How does Java handle integer underflows and overflows and how would you check for it?
(12 answers)
Closed 7 years ago.
I initialized a double a with Math.pow(10,24).
Now I need to convert double a to int b:
a = Math.pow(10,24)
int b = (int)a;
System.out.println(a);
System.out.println(b);
System prints out :
1.0E24
2147483647
This result is obviously not correct.
It's because Integer has limit INT_MAX - 2147483647
This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Closed 7 years ago.
I wanna do this math in java:
int index = 3 * (9568/20001);
in my calculator it shows 3 *( 0.47837608... ) which is 1.43512824..
but, In Java that always give me 0, even I were trying use format, or java.lang.Math.round.
The first postion int 1 of 1.43512824 is what I want to get.
Try this
int index = (int)3 * (9568.0/20001);
Because an integer divided by an integer gives a integer in java thus your answer will not be accurate. If you write 9568.0/20001 it gives a double result and so result is more accurate.