Here's a Java cheat sheet covering key concepts, data types, operators, data structures, and other useful information. This guide should help you with Java basics, especially if you’re preparing for coding interviews.
Java Cheat Sheet
Data Types
-
Primitive Data Types:
int: 4 bytes, stores whole numbers from -2,147,483,648 to 2,147,483,647.byte: 1 byte, stores whole numbers from -128 to 127.short: 2 bytes, stores whole numbers from -32,768 to 32,767.long: 8 bytes, stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.float: 4 bytes, stores fractional numbers, sufficient for storing 6 to 7 decimal digits.double: 8 bytes, stores fractional numbers, sufficient for storing 15 decimal digits.boolean: 1 bit, storestrueorfalse.char: 2 bytes, stores a single 16-bit Unicode character.
-
Reference Data Types:
- Arrays, Classes, Interfaces, Strings
Operators and Precedence
-
Arithmetic Operators:
+(Addition)-(Subtraction)*(Multiplication)/(Division)%(Modulus)
-
Relational Operators:
==(Equal to)!=(Not equal to)>(Greater than)<(Less than)>=(Greater than or equal to)<=(Less than or equal to)
-
Logical Operators:
&&(Logical AND)||(Logical OR)!(Logical NOT)
-
Bitwise Operators:
&(Bitwise AND)|(Bitwise OR)^(Bitwise XOR)~(Bitwise NOT)<<(Left shift)>>(Right shift)>>>(Unsigned right shift)
-
Assignment Operators:
=(Assignment)+=(Add and assign)-=(Subtract and assign)*=(Multiply and assign)/=(Divide and assign)%=(Modulus and assign)
-
Operator Precedence:
- Highest to lowest:
(),[],.,++/--,*///%,+/-,<</>>/>>>,</<=/>/>=,==/!=,&,^,|,&&,||,? :,=,+=,-=...
- Highest to lowest:
Data Structures
-
Arrays:
int[] nums = {1, 2, 3}; nums.length; // Get length nums[0]; // Access element -
ArrayList:
import java.util.ArrayList; ArrayList<Integer> list = new ArrayList<>(); list.add(1); // Add element list.get(0); // Access element list.size(); // Get size list.remove(0); // Remove element list.clear(); // Clear list -
HashMap:
import java.util.HashMap; HashMap<String, Integer> map = new HashMap<>(); map.put("key", 1); // Insert key-value pair map.get("key"); // Access value by key map.remove("key"); // Remove key-value pair map.size(); // Get size map.containsKey("key"); // Check if key exists map.clear(); // Clear map -
HashSet:
import java.util.HashSet; HashSet<Integer> set = new HashSet<>(); set.add(1); // Add element set.contains(1); // Check if element exists set.remove(1); // Remove element set.size(); // Get size set.clear(); // Clear set
Important Data Structures for Coding Interviews
-
Linked List:
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } // LinkedList operations like add, remove, find, etc. -
Stack:
import java.util.Stack; Stack<Integer> stack = new Stack<>(); stack.push(1); // Push element stack.pop(); // Pop element stack.peek(); // Get top element stack.isEmpty(); // Check if stack is empty -
Queue:
import java.util.LinkedList; import java.util.Queue; Queue<Integer> queue = new LinkedList<>(); queue.add(1); // Add element queue.poll(); // Remove and return head element queue.peek(); // Get head element queue.isEmpty(); // Check if queue is empty -
Priority Queue:
import java.util.PriorityQueue; PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(1); // Add element pq.poll(); // Remove and return the smallest element pq.peek(); // Get the smallest element pq.isEmpty(); // Check if priority queue is empty
Clean Code Tips
-
Docstrings and Comments:
/** * Function to double the value. * @param num The number to double. * @return The doubled value. */ public int double(int num) { return 2 * num; } -
Assertions:
assert x == y : "x and y should be equal"; -
Modularity:
- Break down your code into small, reusable functions.
- Avoid code duplication by using methods and classes.
Miscellaneous
-
Input/Output:
import java.util.Scanner; Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); // Read integer input String s = scanner.nextLine(); // Read string input -
Math Functions:
import java.lang.Math; Math.pow(2, 3); // 2 raised to the power 3 Math.sqrt(16); // Square root of 16 Math.max(1, 2); // Maximum of 1 and 2 Math.min(1, 2); // Minimum of 1 and 2 -
String Methods:
String str = "hello"; str.length(); // Get length str.charAt(0); // Get character at index 0 str.substring(1, 3); // Get substring from index 1 to 3 str.toUpperCase(); // Convert to uppercase str.toLowerCase(); // Convert to lowercase str.equals("hello"); // Check equality
This cheat sheet should provide a quick reference to some of the most commonly used features and functionalities in Java. Feel free to adjust or add any sections as per your needs!
Comments (3)

