SwadhyAI Notes

All Java notes

Java operators

›Short versionOpen if you only have a minute.
  • When both sides of / are ints, the result is an int. 10 / 3 is 3. The fraction is discarded toward zero.
  • Storing that result in a double does not bring the fraction back. One operand has to be floating-point before the division.
  • % is the leftover. 10 % 3 is 1. A remainder of 0 means the division was exact.
  • = stores a value. == compares. They are not interchangeable spellings.
  • && and || skip the right side once the answer is fixed. A call on that side does not run.

An combines values you already have. The arithmetic ones are +, -, *, /, and %. The one that does not do what the fraction bar suggests is .

java
int a = 10;
int b = 3;

System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);

What the console shows

13
7
30
3
1

The is that last 1. a / b is 3 because both names are ints. double ratio = a / b; stores 3.0. The division finished before the widening. a / (double) b is the form that keeps the fraction, about 3.333. Casting after the fact is the trap covered on Java type casting.

Multiplication wraps in the same quiet way addition does. Integer.MAX_VALUE * 2 is an int expression, so it overflows. It does not become a long because the result looks big.

Storing, and the extra character in a comparison

replaces the value under a name. The compound forms are the same store after an operation. On an int, /= still divides as an int.

java
int score = 100;
score += 50;
score -= 30;
score *= 2;
score /= 4;
score %= 7;
System.out.println(score);

The print is 4. The steps are 150, 120, 240, 60, and then 60 % 7, which is 4. A does not change either side. age == 18 is true or false. age = 18 stores 18.

++ and -- change the variable by one. Written in front, ++x changes the variable and the value of the expression is the new number. Written behind, x++ the value of the expression is the old number, and the variable changes afterward. When the expression is the whole statement, the difference is invisible. When you print it, or pass it, the difference is the bug.

java
int x = 5;
System.out.println(++x);
System.out.println(x++);
System.out.println(x);

The lines print 6, then 6, then 7. The middle print shows the value from before the post-increment. The variable is already 7 by the last line.

The side that does not run

&& and || combine booleans. evaluation is why the order of the two sides is part of the meaning, not a matter of taste.

java
int temperature = 25;
boolean sunny = true;
System.out.println(temperature > 20 && sunny);
System.out.println(temperature < 0 || sunny);

* and / happen before + and -. 2 + 3 * 4 is 14, not 20. Parentheses are how you force the other order: (2 + 3) * 4 is 20. Comparisons happen after the arithmetic, and && happens before ||. When the order is the point of the line, write the parentheses anyway. The next reader should not have to remember the table.

int a = 10; int b = 3; What are a / b and a % b? What is (double) (a / b)?

a / b is 3 and a % b is 1. (double) (a / b) is 3.0. The parentheses divide two ints first, and the cast only widens the 3 that is left. (double) a / b is the expression that keeps the fraction.