Java Quick Reference Cheat Sheet

Java, a popular object-oriented programming language, is widely used for building enterprise-scale applications. Whether you're a seasoned developer or just starting out, this quick reference cheat sheet will help you brush up on essential Java concepts and syntax.

Java Basics
Java is a statically-typed, object-oriented language that follows the "write once, run anywhere" (WORA) principle. It's known for its rich API and extensive libraries like Java Collections Framework and Java I/O.

Hello World in Java
Here's the classic "Hello, World!" program in Java:

public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Variables and Data Types
Java is a strongly-typed language, meaning you must declare the type of a variable before you can use it. Here are the eight primitive data types in Java:
- byte
- short
- int
- long
- float
- double
- boolean
- char

You can also use reference types to create objects and arrays.
Variable Declaration and Initialization
Here's how to declare and initialize variables in Java:

int x = 10; // declaration and initialization int y; // declaration only y = 20; // initialization
Operators
Java supports various operators for performing operations on variables and values. Here are some common ones:




















| Operator | Description | Example |
|---|---|---|
| + | Addition | x + y |
| - | Subtraction | x - y |
| * | Multiplication | x * y |
| / | Division | x / y |
| % | Modulus (remainder) | x % y |
Control Structures
Java provides several control structures for making decisions and looping through code. Here are a few examples:
if-else Statement
Here's a simple if-else statement that checks if a number is even or odd:
int num = 7;
if (num % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
for Loop
Here's a for loop that prints numbers from 1 to 5:
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
Classes and Objects
In Java, everything is an object or a class. Here's a simple class declaration and how to create an object from it:
public class Person {
String name;
int age;
public void sayHello() {
System.out.println("Hello, I'm " + name + " and I'm " + age + " years old.");
}
}
// Creating an object of Person class
Person person1 = new Person();
person1.name = "John Doe";
person1.age = 30;
person1.sayHello(); // Output: Hello, I'm John Doe and I'm 30 years old.
Important Java Keywords
Here are some important Java keywords that you should know:
- abstract
- continue
- for
- new
- switch
- break
- goto
- package
- synchronized
- case
- if
- private
- this
- catch
- implements
- protected
- throw
- class
- import
- public
- throws
- const
- instanceof
- return
- transient
- default
- int
- short
- try
- do
- interface
- static
- true
- while
- false
- long
- strictfp
- void
This quick reference cheat sheet covers the basics of Java, from variables and data types to control structures and important keywords. Keep it handy for a quick refresher, and happy coding!