Java · Topic 1 of 8
Syntax, types and operators
What you will learn
Java is statically typed: every variable has a type the compiler checks. Understanding primitives, how arithmetic and conversions behave, and how operators evaluate lets you predict what code does and avoid subtle bugs.
Program structure and names
Execution starts at public static void main(String[] args). Variable names may contain letters, digits, underscore and $, but cannot start with a digit or be a keyword, so _count is fine. Use final to make a constant that cannot be reassigned.
Since Java 10, var lets the compiler infer a local variable's type from its initializer, for example var list = new ArrayList<String>(). It needs an initializer and works only for locals.
Example
Program structure and names
public class Main {
public static void main(String[] args) {
final int MAX = 10; // constant
var names = new java.util.ArrayList<String>();
System.out.println(MAX + " " + names.size());
}
}Primitive types and defaults
The primitives are byte, short, int, long, float, double, char and boolean. Integer literals are int and decimal literals like 3.14 are double. Instance fields get defaults (0, 0.0, false, null), while local variables must be assigned before use.
int wraps around on overflow: Integer.MAX_VALUE + 1 becomes the minimum value. Casting a larger integer to byte keeps only the low 8 bits, so (byte) 130 is -126. Casting a double to int truncates toward zero, so (int) -2.9 is -2.
Integer arithmetic
Dividing two ints gives an int with the fraction dropped: 9 / 2 is 4, and assigning that to a double gives 4.0. The result of % takes the sign of the left operand, so -9 % 4 is -1 and -9 / 4 is -2.
Arithmetic on byte, short and char promotes operands to int. Because of that, a char plus 1 is an int (so 'a' + 1 is 98), and two chars added together are a number, not text. Compound assignment such as b += 1 includes an implicit cast, so it compiles for a byte, while b = b + 1 does not.
In an expression, 1000 * 1000 * 1000 * 10 is computed in int and overflows before being widened to long. Write 10L to force long arithmetic.
Example
Integer arithmetic
int a = 9 / 2; // 4
double d = 9 / 2; // 4.0, division happened first
byte b = 10;
b += 1; // OK, implicit cast
long big = 1000L * 1000 * 1000 * 10; // no overflow
System.out.println(a + " " + d + " " + b + " " + big);Operators and evaluation order
x++ yields the old value and then increments; ++x increments first. With x = x++ the increment is overwritten by the old value. The && and || operators short-circuit, so the right side is skipped when the left decides the result.
Shifts use only the low 5 bits of the distance for int, so 1 << 33 equals 1 << 1. >> keeps the sign; >>> fills with zeros. The ternary condition ? 1 : 2.0 promotes both branches to double, giving 1.0.
Strings and comparison
+ is evaluated left to right: once a String is involved, the rest is concatenated. So 1 + 2 + "x" gives 3x. Use equals to compare string contents; == compares references. String literals are interned, so two identical literals are the same object, but new String("hi") is not.
For Integer objects, == compares references, and values from -128 to 127 are cached, so use equals for wrapper types.
Example
Strings and comparison
String a = "hi", c = new String("hi");
System.out.println(a.equals(c)); // true
System.out.println(a == c); // false
System.out.println(1 + 2 + "x" + 1 + 2); // 3x12Key points
- Decimal literals are double; integer literals are int.
- Integer division truncates, and % takes the sign of the left operand.
- byte, short and char promote to int in arithmetic; compound assignment casts back.
- int overflow wraps around silently.
- Use equals for String and wrapper comparison, not ==.
- && and || short-circuit; x++ returns the old value.
Common mistakes
- Expecting 5 / 2 to produce 2.5.
- Comparing strings or Integer objects with ==.
- Forgetting that int arithmetic overflows before assignment to long.
- Assuming 'a' + 'b' concatenates text instead of adding numbers.
Lesson complete
Ready to check what you learned?
Swipe left or right, or use the buttons.