Keywords
Special reserved terms with predefined meaning. Examples include class for defining class structures.
Identifiers
User-defined names for variables, constants, and other entities. Must follow:
- Composition: Letters, digits, underscores (_), dollar signs ($)
- Cannot begin with a digit
- Naming conventions:
- Classes: PascalCase (each word capitalized)
- Variables: camelCase (first word lowercase, subsequent words capitalized)
- Packages: lowercase
- Constants: UPPER_SNAKE_CASE
Comments
Non-executable annotations for code documentation:
- Single-line:
// comment - Multi-line:
/* comment */ - Rules: Single-line comments can nest; multi-line comments cannot nest within other multi-line comments.
Constants and Variables
Constants
Immutable values:
- Integer:
42,100 - Floating-point:
3.14,0.99 - Character: Single character in single quotes (
'A','7') - Boolean:
true,false - String: Sequence in double quotes (
"text",""for empty)
Escape Sequences
Special character representations using backslash (\):
String path = "C:\\Program Files\\App";
Variables
Named storage locations:
// Declaration then assignment
int counter;
counter = 5;
// Combined declaration and assignment
double temperature = 23.5;
Data Types
Primiitve Types
- Integral:
byte(8-bit),short(16-bit),int(32-bit),long(64-bit) - Floating-point:
float(32-bit),double(64-bit) - Character:
char(16-bit Unicode) - Boolean:
boolean
Reference Types
All non-primitive types (objects, arrays).
Operators
Type Conversion
- Implicit: Smaller to larger types (e.g.,
inttodouble) - Explicit (casting): Larger to smaller requires
(target_type)value
Variable Scope
Defined within curly braces {}; accessible from declaration point to end of enclosing block.
Arithmetic Operators
- Addition (
+), subtraction (-), multiplication (*), division (/) - Modulus (
%):num % 2checks even/odd - Increment (
++), decrement (--) - String concatanation:
+with string operand
Assignment Operators
=, +=, -=, *=, /=, %=
Comparison Operators
>, <, >=, <=, ==, != → yield boolean results
Logical Operators
- AND:
&(full evaluation),&&(short-circuit) - OR:
|(full evaluation),||(short-circuit) - NOT:
! - XOR:
^
Ternary Operator
condition ? valueIfTrue : valueIfFalse
Control Flow
Conditional Statements
If-Else
if (condition) {
// code
} else if (altCondition) {
// code
} else {
// fallback
}
Switch
switch (variable) {
case 1:
// action
break;
case 2:
// action
break;
default:
// default action
}
Supported types: byte, short, char, int, String, enums.
Loops
While Loop
while (condition) {
// repeated code
}
Do-While Loop
do {
// code (executes at least once)
} while (condition);
For Loop
for (init; condition; update) {
// loop body
}
Loop Control
break: Exit loop/switchcontinue: Skip to next iteration
Random Numbers
import java.util.Random;
Random rand = new Random();
int diceRoll = rand.nextInt(6) + 1; // 1-6
Arrays
Fixed-size containers for homogeneous data.
Declaration
int[] scores = new int[5]; // 5-element integer array
Memory: Stack holds reference, heap stores elements.
Operations
Traversal
for (int idx = 0; idx < array.length; idx++) {
System.out.println(array[idx]);
}
Find Extremes
int[] data = {34, 12, 89, 5};
int max = data[0];
int min = data[0];
for (int i = 1; i < data.length; i++) {
if (data[i] > max) max = data[i];
if (data[i] < min) min = data[i];
}
Methods
Reusable code blocks.
Definition
access_modifier returnType methodName(paramType paramName) {
// logic
return result; // if non-void
}
voidindicates no return value- Parameters pass input data
Invocation
result = calculateSum(5, 10);
Overloading
Multiple methods with same name but different parameters:
int add(int a, int b) { ... }
double add(double a, double b) { ... }