Java is a popular programming language that has been around since the mid-1990s. It was created by James Gosling and his team at Sun Microsystems, which was later acquired by Oracle Corporation. Java is an object-oriented language that is designed to be platform-independent, meaning that it can run on any operating system without the need for any modifications.
Java is used in a wide range of applications, from web development to mobile app development, and is also used in the development of enterprise-level applications. It is a versatile language that can be used for a variety of purposes, including creating desktop applications, web applications, and even games.
This cheat sheet provides a quick reference for Java programming language. It is divided into different tables with headlines separating them.
Basic Syntax
Syntax
Description
public static void main(String[] args)
The main method that is the entry point of a Java program
System.out.println(""Hello, World!"");
Prints “”Hello, World!”” to the console
// This is a single-line comment
Single-line comment
/* This is a multi-line comment */
Multi-line comment
int x = 5;
Declares an integer variable named x and assigns it the value 5
String name = ""John"";
Declares a string variable named name and assigns it the value “”John””
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
32-bit IEEE 754 floating point
double
64-bit IEEE 754 floating point
boolean
true or false
char
16-bit Unicode character
Operators
Operator
Description
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus
++
Increment
--
Decrement
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to
&&
Logical AND
||
Logical OR
!
Logical NOT
Control Statements
Statement
Description
if
Executes a block of code if a specified condition is true
if-else
Executes a block of code if a specified condition is true, and another block of code if it is false
switch
Evaluates an expression and executes code based on the value of the expression
while
Executes a block of code as long as a specified condition is true
do-while
Executes a block of code at least once, and then continues to execute it as long as a specified condition is true
for
Executes a block of code a specified number of times
break
Terminates a loop or switch statement
continue
Skips the current iteration of a loop
Arrays
Syntax
Description
int[] numbers = {1, 2, 3, 4, 5};
Declares an integer array named numbers and initializes it with the values 1, 2, 3, 4, and 5
int[] numbers = new int[5];
Declares an integer array named numbers with a length of 5
numbers[0] = 1;
Assigns the value 1 to the first element of the numbers array
int length = numbers.length;
Gets the length of the numbers array
Classes and Objects
Syntax
Description
public class MyClass { }
Declares a public class named MyClass
MyClass obj = new MyClass();
Creates an object of the MyClass class
public void myMethod() { }
Declares a public method named myMethod
obj.myMethod();
Calls the myMethod method on the obj object
public int myField = 5;
Declares a public integer field named myField with a value of 5
obj.myField = 10;
Assigns the value 10 to the myField field of the obj object
Exception Handling
Syntax
Description
try { }
Contains the code that might throw an exception
catch (Exception e) { }
Catches the exception and handles it
finally { }
Contains the code that is always executed, regardless of whether an exception was thrown or not
throw new Exception(""Error message"");
Throws an exception with the specified error message