Types, Variables, Operators, and Control Flow
Master Java primitive and reference types, conversions, operators, expressions, branching, loops, arrays, and command-line data.
Primitive Types vs Reference Types
Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. Primitive variables hold values according to the language's rules. Reference variables hold references to objects, arrays, or null.
int is the usual integral type. Use long for larger integer ranges, double for general floating-point calculations, and boolean for true/false state. char represents a UTF-16 code unit.
int count = 42;
long population = 8_000_000_000L;
double price = 19.95;
boolean active = true;
char grade = 'A';
String name = "Suriya";
int[] scores = {90, 85, 95};
Wrapper classes such as Integer, Long, and Boolean represent primitive values as objects and are required in many generic APIs.
- Primitive and reference types have different semantics.
Stringand arrays are reference types.- Choose numeric types based on range and precision requirements.
Conversions, Casting, Autoboxing, and Unboxing
Java performs some conversions automatically when they are safe, such as widening int to long. Narrowing conversions require an explicit cast and can lose information.
int n = 10;
long big = n;
int smaller = (int) big;
Integer boxed = n;
int value = boxed;
Boxing is convenient but creates object values when an object representation is needed. It can also introduce NullPointerException during unboxing if a wrapper is null.
Numeric promotion can produce surprising results. 5 / 2 is 2 because both operands are integers; 5 / 2.0 is 2.5.
A nullable Integer is not interchangeable with an int; unboxing null throws NullPointerException.
- Widening conversions generally preserve the value.
- Narrowing casts may truncate or overflow.
- Autoboxing is convenient but does not remove allocation and nullability concerns.
Operators and Expressions
Java supports arithmetic, comparison, logical, bitwise, shift, assignment, conditional, and increment/decrement operators.
int a = 10;
int b = 3;
int sum = a + b;
int remainder = a % b;
boolean result = a > b && b > 0;
int max = a > b ? a : b;
&& and || are short-circuiting operators. == compares primitive values directly but compares references by identity when both operands are references. Use .equals() for logical value equality when the type defines it appropriately.
&&and||short-circuit.==is not a general replacement for.equals().- Use parentheses to make non-obvious precedence explicit.
Branching and Loops
Java provides if/else, switch, for, enhanced for, while, and do-while. Modern Java also supports switch expressions, which can return a value.
int score = 82;
String result = switch (score / 10) {
case 10, 9 -> "Excellent";
case 8 -> "Good";
case 7 -> "Average";
default -> "Needs improvement";
};
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Prefer the construct that expresses intent most clearly. Enhanced for is ideal for straightforward iteration. Streams are useful when the operation is naturally a data transformation pipeline; they are not automatically better than a loop.
- Use switch expressions when a branch naturally produces a value.
- Enhanced
forimproves readability for collection traversal. - Choose loops and streams based on clarity and semantics.