Lesson 285 of 296
Java basics for TypeScript developers
What you will learn
- What Java, the JDK and the JVM are
- A first program: class and main
- Static types, primitives and reference types
- Strings
- Control flow, switch and arrays
- Methods, overloading and var
On this page
What Java is
Java is a statically typed, object-oriented language used heavily in banking, enterprise back ends (often with the Spring Boot framework) and Android. Its motto is "write once, run anywhere":
- The compiler
javacturns.javafiles into bytecode (.classfiles). - The JVM (Java Virtual Machine) runs the bytecode on any operating system, compiling hot code to machine code while it runs.
| Word | Meaning |
|---|---|
| JDK | Java Development Kit: compiler and tools, what developers install |
| JRE / runtime | What is needed to run Java programs |
| JVM | The engine that runs bytecode |
Java releases a new version every six months. Some are LTS (long-term support) releases that companies stay on for years. Java 25 (September 2025) is the current LTS, after Java 21.
A first program
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java");
int a = 7;
int b = 2;
System.out.println(a / b); // 3 (int division drops the fraction)
System.out.println(a / 2.0); // 3.5
System.out.println(a % b); // 1
}
}Rules:
- All code lives inside a class. A public class must be in a file with the same name:
Hello.java. - The program starts at
public static void main(String[] args). - Statements end with
;and blocks use{ }. - Run it with
java Hello.java, or compile withjavac Hello.javathenjava Hello.
Java 25 also allows a shorter form for small programs and learning (a main method without the class and static), but most real and interview code still uses the full form above.
Static types
Every variable has a type fixed at compile time, and the compiler checks it, like TypeScript but stricter: there is no any, and types exist at runtime too.
Primitive types hold simple values directly:
| Type | Holds | Example |
|---|---|---|
| int | Whole numbers up to about 2.1 billion | int age = 30; |
| long | Larger whole numbers | long views = 5_000_000_000L; |
| double | Decimal numbers | double price = 9.99; |
| boolean | true / false | boolean active = true; |
| char | One character, single quotes | char grade = 'A'; |
| byte, short, float | Smaller number types | Rarely used in app code |
Everything else is a reference type: String, arrays, and any object. A reference variable can be null.
Integer overflow is a classic trap: int wraps around silently.
public class Overflow {
public static void main(String[] args) {
int big = Integer.MAX_VALUE;
System.out.println(big); // 2147483647
System.out.println(big + 1); // -2147483648 wraps around!
long safe = (long) big + 1;
System.out.println(safe); // 2147483648
System.out.println(0.1 + 0.2); // 0.30000000000000004
}
}Strings
String is a class. Strings are immutable.
public class Strings {
public static void main(String[] args) {
String name = "Sam";
System.out.println(name.length()); // 3
System.out.println(name.charAt(0)); // S
System.out.println(name.toUpperCase()); // SAM
System.out.println(name.contains("am")); // true
System.out.println("a,b,c".split(",").length); // 3
System.out.println(String.format("%s is %d", name, 30)); // Sam is 30
String text = """
Text blocks keep
line breaks""";
System.out.println(text);
}
}The """ text block is like a TypeScript template literal across lines (without ${}).
Most important trick question: compare strings with .equals(), not == (Lesson 288).
Control flow, switch and arrays
public class Flow {
public static void main(String[] args) {
int score = 72;
if (score >= 80) {
System.out.println("A");
} else if (score >= 60) {
System.out.println("B");
} else {
System.out.println("C");
}
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
}
System.out.println();
int[] nums = {4, 8, 15}; // fixed-size array
for (int n : nums) { // "for each" loop
System.out.print(n + " ");
}
System.out.println(nums.length); // 3 (a field, not a method)
String day = "SAT";
String type = switch (day) { // switch expression
case "SAT", "SUN" -> "weekend";
default -> "weekday";
};
System.out.println(type); // weekend
String label = score > 50 ? "pass" : "fail";
System.out.println(label); // pass
}
}Arrays have a fixed size. For a growing list use ArrayList (Lesson 287).
Methods, overloading and var
public class Methods {
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) { // overloading: same name, different parameters
return a + b;
}
static void greet(String name) { // void: returns nothing
System.out.println("Hi " + name);
}
public static void main(String[] args) {
System.out.println(add(2, 3)); // 5
System.out.println(add(2.5, 3.0)); // 5.5
greet("Ana");
var total = add(10, 20); // var: the compiler infers int
System.out.println(total); // 30
}
}Java has no default parameter values; overloading is used instead. var only works for local variables and the type is still fixed.
How to say it in an interview
"Java compiles to bytecode that runs on the JVM, so the same build runs on any OS. It is statically typed with primitives like int, long, double and boolean, and reference types like String and objects, which can be null. I watch for integer overflow and int division, compare strings with equals, use switch expressions and enhanced for loops, ArrayList instead of fixed arrays when the size changes, and overloading instead of default parameters. Java 25 is the current LTS release."
Key words
| Word | Meaning |
|---|---|
| JDK / JVM | Developer kit / the engine that runs bytecode |
| Bytecode | Compiled Java code the JVM runs |
| LTS | A long-term support release |
| Primitive type | A simple value type like int or boolean |
| Reference type | A type whose variables point to objects |
| Overloading | Several methods with the same name and different parameters |
| Overflow | A number going past its limit and wrapping around |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.