■ Java Quick Cheat Sheet for Interviews
1. What is Java?
- High-level, OOP, platform-independent language (WORA).
- Compiled to bytecode, runs on JVM.
Interview Tip: "Java is both compiled and interpreted."
2. History of Java
- Developed by Sun Microsystems (1991), James Gosling.
- Initially "Oak", renamed Java in 1995.
- Oracle owns Java now.
3. Features of Java
- Simple, Object-Oriented, Platform Independent.
- Robust, Secure, Multithreaded, Distributed, Portable.
4. JDK, JRE, JVM
- JVM: Runs bytecode.
- JRE: JVM + libraries.
- JDK: JRE + compilers, tools.
Tip: JVM is platform-dependent, bytecode is platform-independent.
5. Java Versions
- LTS: 8, 11, 17, 21.
- Features: Lambda (8), Modules (9), Switch Expr (12), Records (14), Pattern Matching (16+).
6. JAVA_HOME & Path
- JAVA_HOME → JDK installation dir.
- PATH → add bin dir for javac/java.
7. Compilation
- Source (.java) → javac → Bytecode (.class).
- JVM executes via JIT.
8. First Java Program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
9. Data Types
- Primitive (byte, short, int, long, float, double, char, boolean).
- Default values: int=0, float=0.0f, boolean=false, object=null.
Rule: char stores UTF-16.
10. Multiple Classes in File
- Only one public class allowed, filename must match.
11. Type Casting
- Widening: auto (int→long).
- Narrowing: explicit (double→int).
Rule: byte+byte → int.
12. Tokens
- Identifiers, Keywords, Literals, Operators, Separators.
13. Variables
- Local, Instance, Static.
Tip: Uninitialized local vars → compile error.
14. Constants / final
- final variable = constant.
- final method = no override.
- final class = no extend.
15. Identifiers
- Rules: letters, digits, _, $, not start with digit, not keyword.
- Case-sensitive.
- Conventions: Class=PascalCase, var=camelCase, CONST=UPPERCASE.
16. Keywords
- 67 reserved words (public, static, void, class, return, etc).
Tip: true/false/null are literals, not keywords.
17. Operators (Cheat Sheet)
- Arithmetic: + - * / %
- Unary: ++ -- + - ! ~
- Assignment: =, +=, -=, etc.
- Relational: < > <= >=
- Equality: == !=
- Logical: && || !
- Bitwise: & | ^ ~ << >> >>>
- Ternary: ? :
- instanceof
Special Rules:
- int/0 → ArithmeticException; double/0.0 → Infinity/NaN.
- % sign follows dividend.
- byte+byte → int (needs cast).
- b += 1 compiles (auto-cast), b = b+1 fails.
- NaN != NaN, NaN == NaN is false.
- Integer caching: -128..127 reused.
- Shift counts masked (int: mod 32, long: mod 64).
- ~x = -x - 1.
- &&/|| short-circuit, &/| always evaluate.
Example Traps:
int i=1; i = i++ + ++i; // i=4
System.out.println("a"+1+2); // a12
System.out.println("a"+(1+2)); // a3