Java Cheat Sheet

In Java

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

SyntaxDescription
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 commentSingle-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 TypeDescription
byte8-bit signed two’s complement integer
short16-bit signed two’s complement integer
int32-bit signed two’s complement integer
long64-bit signed two’s complement integer
float32-bit IEEE 754 floating point
double64-bit IEEE 754 floating point
booleantrue or false
char16-bit Unicode character

Operators

OperatorDescription
+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

StatementDescription
ifExecutes a block of code if a specified condition is true
if-elseExecutes a block of code if a specified condition is true, and another block of code if it is false
switchEvaluates an expression and executes code based on the value of the expression
whileExecutes a block of code as long as a specified condition is true
do-whileExecutes a block of code at least once, and then continues to execute it as long as a specified condition is true
forExecutes a block of code a specified number of times
breakTerminates a loop or switch statement
continueSkips the current iteration of a loop

Arrays

SyntaxDescription
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

SyntaxDescription
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

SyntaxDescription
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

References

#