Introduction to Java
Java is a statically typed, object-oriented language designed to run anywhere through the Java Virtual Machine (JVM): "write once, run anywhere".
JDK, JRE and JVM
| Term | Meaning |
|---|---|
| JVM | Runs compiled bytecode; includes the garbage collector and JIT compiler |
| JRE | JVM plus standard libraries |
| JDK | JRE plus development tools (javac, jar, jshell) |
Compilation model
Hello.java --javac--> Hello.class (bytecode) --JVM--> machine code
Hello, Java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, PrepVerse!");
}
}
javac Hello.java
java Hello
Program structure
- Every class lives in a file with the same name (for public classes).
mainis the entry point:public static void main(String[] args).- Packages organise classes (
package com.prepverse.app;). - Statements end with
;and blocks use{ }.
Primitive types
| Type | Size | Example |
|---|---|---|
byte / short / int / long | 8 / 16 / 32 / 64 bit | int age = 21; |
float / double | 32 / 64 bit | double pi = 3.14; |
char | 16 bit | char c = 'A'; |
boolean | true or false | boolean ok = true; |
Everything else is a reference type (objects, arrays, String). String is immutable.
Memory
- Stack: method frames and local variables.
- Heap: objects, managed by the garbage collector.
- You never free memory manually, but you can still leak it by holding references.
Loading Comments...