Varia Flashcards
Tehe kahe int vahel
Tulemus alati int
Tehtes on üks double
Tulemus alati double
int a = 10; int b = 3; (double) a / b =
double 3.3333333333333335
int a = 10; int b = 3; (double) (a / b) =
double 3.0
double aa = 10.0; double bb = 3.0; (int) aa / bb =
double 3.3333333333333335
double aa = 10.0; double bb = 3.0; (int) aa / (int) bb =
int 3
double aa = 10.0; double bb = 3.0; (int) (aa / bb) =
int 3
int a = 10 var c = 3 var x = (double) (a / b) =
double 3.0
In Java, when declaring a variable as var, the compiler will
look for the type of the variable on the right side of the
expression. Here it can see it’s int 3, so var c is same as int
3. And a / b becomes an equation between two ints 10 / 3 = 3.
Though the casting (double) makes them become 3.0. And var x
becomes the equivalent of a double 3.0.
ValueRange
maksimum
on ___
Kaasaarvatud.
ValueRange.of(0, 2).isValidIntValue(2) == true
Mida prindib kood?
for (int i = 1; i++ < 5; i--) {
System.out.print(i);
}
If it’s i++
, i=1 is checked, but immediately after it passes into the loop, it becomes 2 and 2 is printed. i—Only happens in the end of the loop. If it were ++i
, it would become 2 already prior to the condition check.
Kuidas kontrollida, et BigDecimal money
väärtus ei ole väiksem kui 0?
money.compareTo(BigDecimal.ZERO) < 0
Comparison returns -1 if the first value is less than the other value, 0 if equal,
1 if greater.
Round double value moviePrice
to 2 decimal places using BigDecimal
.
// Round to 2 decimal places:
// 13.3555 -> 13.36
return moviePrice.setScale(2, RoundingMode.HALF_UP);
It’s the best way to round everything, but project has to use Java 17 to do so.
What are A, B, C values in the end?
int A = 3, B = 3, C = 0; while (A-- > 0) { while (B-- > 0) { if (A + B == A) { C += A; } } if (A + B == B) { C += B; } }
A = -1, B = -3, C = -1
- First while loop: A=3 passes and is decremented to 2.
- Second while loop: B=3 passes and is decremented to 2.
- If sentence (2 + 2 == 2): Doesn’t start.
- Second while loop: B=2 passes and is decremented to 1.
- If sentence (2 + 1 == 2): Doesn’t start.
- Second while loop: B=1 passes and is decremented to 0.
- If sentence (2 + 0 == 2): Starts. C = 2.
- SECOND WHILE LOOP IS CHECKED ONE LAST TIME. B=0 doesn’t pass but is decremented to -1.
- If sentence (2 - 1 = -1): Doesn’t start.
- First while loop: A=2 passes and is decremented to 1.
- Second while loop: B=-1 doesn’t pass but is decremented to -2.
- If sentence (1 - 2 = -2): Doesn’t start.
- First while loop: A=1 passes and is decremented to 0.
- Second while loop: B=-2 doesn’t pass but is decremented to -3
- If sentence (0 - 3 = -3). Starts. C = -1.
- FIRST WHILE LOOP IS CHECKED ONE LAST TIME. A=0 doesn’t pass but is decremented to -1.