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

  1. 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, stores true or false.
    • char: 2 bytes, stores a single 16-bit Unicode character.
  2. Reference Data Types:

    • Arrays, Classes, Interfaces, Strings

Operators and Precedence

  1. Arithmetic Operators:

    • + (Addition)
    • - (Subtraction)
    • * (Multiplication)
    • / (Division)
    • % (Modulus)
  2. Relational Operators:

    • == (Equal to)
    • != (Not equal to)
    • > (Greater than)
    • < (Less than)
    • >= (Greater than or equal to)
    • <= (Less than or equal to)
  3. Logical Operators:

    • && (Logical AND)
    • || (Logical OR)
    • ! (Logical NOT)
  4. Bitwise Operators:

    • & (Bitwise AND)
    • | (Bitwise OR)
    • ^ (Bitwise XOR)
    • ~ (Bitwise NOT)
    • << (Left shift)
    • >> (Right shift)
    • >>> (Unsigned right shift)
  5. Assignment Operators:

    • = (Assignment)
    • += (Add and assign)
    • -= (Subtract and assign)
    • *= (Multiply and assign)
    • /= (Divide and assign)
    • %= (Modulus and assign)
  6. Operator Precedence:

    • Highest to lowest: (), [], ., ++/--, *///%, +/-, <</>>/>>>, </<=/>/>=, ==/!=, &, ^, |, &&, ||, ? :, =, +=, -=...

Data Structures

  1. Arrays:

    int[] nums = {1, 2, 3};
    nums.length; // Get length
    nums[0]; // Access element
  2. 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
  3. 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
  4. 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

  1. Linked List:

    class Node {
        int data;
        Node next;
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    
    // LinkedList operations like add, remove, find, etc.
  2. 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
  3. 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
  4. 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

  1. 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;
    }
  2. Assertions:

    assert x == y : "x and y should be equal";
  3. Modularity:

    • Break down your code into small, reusable functions.
    • Avoid code duplication by using methods and classes.

Miscellaneous

  1. 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
  2. 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
  3. 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 (2)