Skip to main content

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​

TermMeaning
JVMRuns compiled bytecode; includes the garbage collector and JIT compiler
JREJVM plus standard libraries
JDKJRE 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).
  • main is the entry point: public static void main(String[] args).
  • Packages organise classes (package com.prepverse.app;).
  • Statements end with ; and blocks use { }.

Primitive types​

TypeSizeExample
byte / short / int / long8 / 16 / 32 / 64 bitint age = 21;
float / double32 / 64 bitdouble pi = 3.14;
char16 bitchar c = 'A';
booleantrue or falseboolean 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.