Syntax Basics

Literals

Integer Literals

FormatExampleDescription
Decimal42Base 10
Hexadecimal0x2ABase 16, prefix 0x
Binary0b101010Base 2, prefix 0b
Octal052Base 8, prefix 0
Long42L64-bit with L suffix
Underscores1_000_000Readability separators

Floating-Point Literals

FormatExampleDescription
Double3.1464-bit (default)
Float3.14f32-bit with f suffix
Scientific1.5e10Exponential 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

KeywordDescription
classDeclare a class
interfaceDeclare an interface
enumDeclare an enumeration
recordDeclare a record (Java 14+)
extendsInherit from a class
implementsImplement interfaces

Primitive Types

TypeSizeRange
byte8-bit-128 to 127
short16-bit-32,768 to 32,767
int32-bit-2^31 to 2^31-1
long64-bit-2^63 to 2^63-1
float32-bitIEEE 754 floating-point
double64-bitIEEE 754 floating-point
char16-bitUnicode character
boolean1-bittrue or false
void-No return value

Access Modifiers

ModifierDescription
publicAccessible from anywhere
privateAccessible only within the class
protectedAccessible within package and subclasses

Other Modifiers

ModifierDescription
staticClass-level member
finalCannot be modified/overridden
abstractNo implementation provided
synchronizedThread-safe access
volatileMemory visibility guarantee
transientExcluded from serialization
nativeImplemented in native code
sealedRestricted inheritance (Java 17+)
non-sealedAllow 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

OperatorDescriptionExample
+Addition5 + 3 = 8
-Subtraction5 - 3 = 2
*Multiplication5 * 3 = 15
/Division5 / 3 = 1
%Modulo5 % 3 = 2

Comparison Operators

OperatorDescription
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

Logical Operators

OperatorDescription
&&Logical AND (short-circuit)
||Logical OR (short-circuit)
!Logical NOT

Bitwise Operators

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Signed right shift
>>>Unsigned right shift

Assignment Operators

=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=, >>>=

Unary Operators

OperatorDescription
++xPre-increment
x++Post-increment
--xPre-decrement
x--Post-decrement
+xUnary plus
-xUnary minus

Special Operators

OperatorDescriptionExample
? :Ternary conditionalx > 0 ? "positive" : "non-positive"
instanceofType checkingobj instanceof String
->Lambda arrowx -> x * 2
::Method referenceString::length

Operator Precedence

PrecedenceOperators
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
 */