Java Operators and Expressions: A Beginner’s Guide from Scratch
This article provides a comprehensive walkthrough of Java's operators and expressions, covering arithmetic, assignment, increment/decrement, relational, logical, bitwise, and ternary operators, their syntax, evaluation rules, precedence, and practical code examples to help beginners master Java fundamentals.
Overview
The note introduces Java operators and expressions, explaining that operators are symbols with computational functions and that expressions combine operands and operators according to Java syntax to produce a result.
Operator Categories
Java defines the following operator groups:
Arithmetic operators
Assignment operators
Increment and decrement operators
Relational operators
Logical operators
Bitwise operators
Ternary operator
Arithmetic Operators and Expressions
Common arithmetic operators include +, -, *, /. Integer division truncates the decimal part without rounding: System.out.println(15/2); // prints 7 Remainder (modulo) operator returns the remainder of division: System.out.println(17%2); // prints 1 Dividing by zero is syntactically allowed but throws java.lang.ArithmeticException at runtime.
Assignment Operators
The basic assignment operator = copies the right‑hand value to the left‑hand variable. Assignment expressions can be chained because the operator is right‑associative: a = b = c = 11; Compound assignment operators combine an arithmetic operation with assignment (e.g., +=, -=).
Increment and Decrement Operators
Unary operators ++ and -- increase or decrease a variable by one. They can appear as prefix or postfix:
int a = 10;
int c = a++; // c = 10, a becomes 11
int d = ++a; // a becomes 12, d = 12Prefix form updates the variable before the expression is evaluated; postfix updates after.
Relational Operators
Relational operators ( >, <, >=, <=, ==, !=) compare two values and yield a boolean result. They work with primitive types; for objects, only == and != are applicable.
int a = 2;
System.out.println(a > 1); // trueLogical Operators
Logical operators combine boolean expressions:
AND: && (short‑circuit) and & OR: || (short‑circuit) and | XOR: ^ NOT: ! Short‑circuit operators evaluate the right‑hand side only when necessary.
boolean result = (stock <= 10) && (price > 20);
System.out.println(result);Bitwise Operators
Bitwise operators work on the binary representation of integer operands:
AND: & OR: | XOR: ^ NOT: ~ Left shift: << Right shift: >> (sign‑preserving) and >>> (zero‑fill)
System.out.println(8 & 10); // 8
System.out.println(7 | 8); // 15
System.out.println(9 ^ 17); // 24
System.out.println(~19); // -20
int a = 16;
System.out.println(a << 2); // 64
System.out.println(-14 >> 1); // -7
System.out.println(-14 >>> 1); // 2147483641Ternary Operator
The ternary operator condition ? expr1 : expr2 selects one of two expressions based on a boolean condition:
int score = 75;
System.out.println(score > 59 ? "Pass" : "Fail"); // prints "Pass"Operator Precedence
When an expression contains multiple operators, Java evaluates them according to a defined precedence table. Parentheses can override precedence and enforce a specific evaluation order.
Using parentheses ensures that lower‑precedence operators are evaluated first, simplifying reasoning about complex expressions.
Lisa Notes
Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
