Java Chapter 2 – Exam Notes (Based Only on Your
PDF)
1. Java Development Environment
• JDK: Contains JRE + compiler (javac), debugger (jdb), tools for writing and running Java.
• JRE: Contains JVM + libraries; used only to run Java apps.
• IDEs: Eclipse, IntelliJ, NetBeans → editor, compiler, debugger, project manager.
• Build Tools: Maven, Gradle → automate compile, test, package.
• Version Control: Git → track code changes.
2. Java Virtual Machine (JVM)
• JVM reads and executes .class bytecode.
• Platform-independent.
JVM Components:
- Stack: Stores method frames & local variables.
- Heap: Stores objects & arrays.
• JVM performs loading, verification, and execution.
3. Simple Java Program Structure
• Class declaration defines blueprint.
• main() is program entry point.
• [Link] prints output.
• Comments: /* */ for multi-line, // for single-line.
4. Variables & Data Types
• Variable = memory location to store value.
• Declaration: int score; Initialization: score = 4;
• Assignment = copies value into variable memory.
Types of Variables:
- Local (inside method).
- Instance (object-level).
- Static (shared).
Data Types:
- Primitive: byte, short, int, long, float, double, char, boolean.
- Non-primitive: String, Array, Class, Interface, Object.
5. Naming Rules (Identifiers)
• Can use letters, digits, $, _ only.
• Cannot start with a digit.
• Case-sensitive.
• Cannot use reserved words like int, while.
6. Classes & Objects
• Class = template/blueprint for objects.
• Object = instance with state, behavior, identity.
• Fields store data; methods store behavior.
• Objects are stored in heap; references stored in stack.
7. Constructors
• Same name as class.
• No return type.
• Initializes object variables.
• If no constructor provided → Java adds default constructor.
8. 'this' Keyword
• Refers to current object.
• Used to access instance variables, avoid naming conflicts.
• Used to call methods of the same class.
9. Object Creation Methods
• new → normal object creation.
• [Link]() → calls default constructor.
• [Link]() → can call parameterized constructors.
10. Java Input/Output (Scanner)
• nextLine → reads string.
• nextInt → reads integer.
• nextDouble → reads decimal.
• Avoid input mismatch using hasNextInt(), hasNextDouble().
• print = same line; println = new line.
11. Conditional Statements
• if, if-else, if-else-if → check conditions.
• switch → for fixed values.
12. Loops
• for → known number of iterations.
• while → condition checked first.
• do-while → runs at least once.
• enhanced for → iterates arrays.