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
Related
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?
This question already has answers here:
How does double to int cast work in Java
(3 answers)
Closed 4 years ago.
I understand that the result of Math.pow() is a double.
However, why isn't the below code causing an integer overflow when I have explicitly casted the result to an int?
Also, why is the result of both 'a' and 'b' the same i.e 2147483647
int a=(int)(Math.pow(2,377));
int b=(int)(Math.pow(2,32));
System.out.println(a);
System.out.println(b);
If a double is larger than the maximal int value, converting it to int will give you the maximal int value.
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:
"Integer number too large" error message for 600851475143
(8 answers)
Closed 4 years ago.
This is just a short question but I'm still curious. When I initialize a int variable with the highest value 2147483647 it's allright. but when I want to initialize a long and assign it to it's max value, it gives me a "number too large" error
int i = 2147483647;
long j = 9223372036854775807;
9223372036854775807 is an int literal, but is too large to fit into an int, hence the error. You can use a long literal instead by adding L to the end of it:
long j = 9223372036854775807L;
// Here --------------------^
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.