This Java cheatsheet covers some of the most commonly used features and syntax in the Java programming language, including data types, control flow statements, arrays, classes and objects, inheritance, interfaces, and exception handling. Whether you’re a beginner or an senior Java developer, this cheatsheet is a handy reference guide to help you quickly look up key concepts and code snippets.
Data Types:
Data Type | Description |
---|---|
byte | 8-bit signed two’s complement integer |
short | 16-bit signed two’s complement integer |
int | 32-bit signed two’s complement integer |
long | 64-bit signed two’s complement integer |
float | Single-precision 32-bit IEEE 754 floating point |
double | Double-precision 64-bit IEEE 754 floating point |
boolean | true or false |
char | Single 16-bit Unicode character |
Control Flow Statements:
Statement | Description |
---|---|
if-else | Executes a block of code if a certain condition is true, and another block of code if the condition is false |
switch | Evaluates an expression and executes code block based on different cases |
for | Executes a block of code a fixed number of times |
while | Repeats a block of code while a certain condition is true |
do-while | Repeats a block of code at least once, and continues to repeat the block as long as a certain condition is true |
break | Terminates the innermost loop or switch statement |
continue | Skips the current iteration of a loop and continues with the next iteration |
Arrays:
Syntax | Description |
---|---|
dataType[] arrayName; | Declares an array of the specified data type |
arrayName = new dataType[arraySize]; | Creates an array of the specified data type and size |
dataType[] arrayName = {value1, value2, value3, …}; | Creates an array and initializes it with specified values |
Classes and Objects:
Syntax | Description |
---|---|
class className { … } | Defines a class |
className objectName = new className(); | Creates an instance of a class |
objectName.methodName(); | Calls a method on an object |
public static void main(String[] args) { … } | The main method, which is the entry point of a Java program |
Inheritance:
Syntax | Description |
---|---|
class subclassName extends superclassName { … } | Defines a subclass that inherits from a superclass |
super.methodName(); | Calls a method from the superclass |
Interfaces:
Syntax | Description |
---|---|
interface interfaceName { … } | Defines an interface |
class className implements interfaceName { … } | Implements an interface |
Exception Handling:
Syntax | Description |
---|---|
try { … } catch (Exception e) { … } finally { … } | Handles exceptions that might occur in the try block |
throw new Exception(); | Throws an exception |
throws Exception | Declares that a method might throw an exception |
I hope this helps!