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
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:
Understanding recursion [closed]
(20 answers)
In a recursive statment, how does java store the past values?
(3 answers)
Reversing a String with Recursion in Java
(17 answers)
How are the numbers stored, called, and added in this short recursion example?
(3 answers)
Closed 3 years ago.
I'm attempting to create a method that will calculate the sum of a an integer, as the integer is broken down in to single integers.
E.g. 2546 becomes 2, 5, 4, 6. Then I plus those all together.
2 + 5 + 4 + 6 = 17
The method will run recursively.
I'd made this program non-recursively, but in that one, I had a variable to store a sum of the calculation.
public static int calcSum(int n){
if (n>0)
return ((n%10) + calcSum(n/10));
else
return 0;
}
The program works, I just don't understand how the sum is stored.
The sum is not stored in any variable (unless the caller of the recursive method will store the result in some variable).
The recursive method returns the sum to its caller without storing it in a variable:
return ((n%10) + calcSum(n/10));
or returns 0 if the input is 0.
calcSum(2546) returns 6 + calcSum(254)
calcSum(254) returns 4 + calcSum(25)
calcSum(25) returns 5 + calcSum(2)
calcSum(2) returns 2 + calcSum(0)
calSum(0) returns 0
so when the recursion unwinds:
calcSum(2) returns 2 + 0 == 2
calcSum(25) returns 5 + 2 == 7
calcSum(254) returns 4 + 7 == 11
and finally
calcSum(2546) returns 6 + 11 == 17
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I am trying to implement a simple equation in Java but keep getting the wrong answer apparently due to operator precedence which I am unable to understand.
The equation is:
NewMean = ((N-1) / N) * OldMean + (Xn / N)
in a simple example:
N = 6 ; OldMean = 6 ; Xn = 16
So,
NewMean = 5/6 * 6 + 16/6 = 7.6667 (Correct answer)
but in code implementation on Java i get wrong answer (2.6665):
double NewMean = ((N-1)/N)*oldMean + (Xn/N);
If the N variable is type int, then ((N-1) / N) is computed using integer division and will round 5/6 down to 0. Change N to a floating-point type and you should get the correct answer.
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:
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