Syntax Basics
Literals
Integer Literals
| Format | Example | Description |
| Decimal | 42 | Base 10 |
| Hexadecimal | 0x2A | Base 16, prefix 0x |
| Binary | 0b101010 | Base 2, prefix 0b |
| Octal | 052 | Base 8, prefix 0 |
| Long | 42L | 64-bit with L suffix |
| Underscores | 1_000_000 | Readability separators |
Floating-Point Literals
| Format | Example | Description |
| Double | 3.14 | 64-bit (default) |
| Float | 3.14f | 32-bit with f suffix |
| Scientific | 1.5e10 | Exponential notation |
Character and String Literals
char c = 'A';
String s = "Hello, World!";
// Escape sequences
'\t' // Tab
'\n' // Newline
'\r' // Carriage return
'\\' // Backslash
'\'' // Single quote
'\"' // Double quote
'\u0041' // Unicode (A)
Text Blocks (Multi-line Strings)
String html = """
<html>
<body>Hello</body>
</html>
""";
Boolean and Null
boolean flag = true;
boolean other = false;
Object obj = null;
Keywords (58 total)
Type Keywords
| Keyword | Description |
class | Declare a class |
interface | Declare an interface |
enum | Declare an enumeration |
record | Declare a record (Java 14+) |
extends | Inherit from a class |
implements | Implement interfaces |
Primitive Types
| Type | Size | Range |
byte | 8-bit | -128 to 127 |
short | 16-bit | -32,768 to 32,767 |
int | 32-bit | -2^31 to 2^31-1 |
long | 64-bit | -2^63 to 2^63-1 |
float | 32-bit | IEEE 754 floating-point |
double | 64-bit | IEEE 754 floating-point |
char | 16-bit | Unicode character |
boolean | 1-bit | true or false |
void | - | No return value |
Access Modifiers
| Modifier | Description |
public | Accessible from anywhere |
private | Accessible only within the class |
protected | Accessible within package and subclasses |
Other Modifiers
| Modifier | Description |
static | Class-level member |
final | Cannot be modified/overridden |
abstract | No implementation provided |
synchronized | Thread-safe access |
volatile | Memory visibility guarantee |
transient | Excluded from serialization |
native | Implemented in native code |
sealed | Restricted inheritance (Java 17+) |
non-sealed | Allow further extension |
Control Flow Keywords
if, else, for, while, do, switch, case, default, break, continue, return, yield
Exception Keywords
try, catch, finally, throw, throws
Other Keywords
new, this, super, instanceof, var, assert, package, import
Operators
Arithmetic Operators
| Operator | Description | Example |
+ | Addition | 5 + 3 = 8 |
- | Subtraction | 5 - 3 = 2 |
* | Multiplication | 5 * 3 = 15 |
/ | Division | 5 / 3 = 1 |
% | Modulo | 5 % 3 = 2 |
Comparison Operators
| Operator | Description |
== | Equal to |
!= | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater than or equal |
Logical Operators
| Operator | Description |
&& | Logical AND (short-circuit) |
|| | Logical OR (short-circuit) |
! | Logical NOT |
Bitwise Operators
| Operator | Description |
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Signed right shift |
>>> | Unsigned right shift |
Assignment Operators
=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=
Unary Operators
| Operator | Description |
++x | Pre-increment |
x++ | Post-increment |
--x | Pre-decrement |
x-- | Post-decrement |
+x | Unary plus |
-x | Unary minus |
Special Operators
| Operator | Description | Example |
? : | Ternary conditional | x > 0 ? "positive" : "non-positive" |
instanceof | Type checking | obj instanceof String |
-> | Lambda arrow | x -> x * 2 |
:: | Method reference | String::length |
Operator Precedence
| Precedence | Operators |
| 1 (highest) | () [] . |
| 2 | ++ -- + - ! ~ (unary) |
| 3 | * / % |
| 4 | + - |
| 5 | << >> >>> |
| 6 | < <= > >= instanceof |
| 7 | == != |
| 8 | & |
| 9 | ^ |
| 10 | | |
| 11 | && |
| 12 | || |
| 13 | ? : |
| 14 (lowest) | = += -= *= /= ... |
Comments
// Single-line comment
/* Multi-line
comment */
/**
* Documentation comment
* @param x description
* @return description
*/