Java basics: types and syntax
Java is a statically typed, object-oriented language used in large business systems, Android apps, and university courses. Its syntax is stricter than Python's, which catches many mistakes early.
About Java
Java was developed by James Gosling and colleagues at Sun Microsystems and released in 1995 with the slogan 'write once, run anywhere'. Java source code compiles to bytecode that runs on the Java Virtual Machine (JVM), so the same program runs on any platform with a JVM. Oracle has owned Java since acquiring Sun in 2010.
Structure of a program
Every Java program lives inside classes. The entry point is public static void main(String[] args). A minimal program is: public class Hello { public static void main(String[] args) { System.out.println("Hello"); } }. The file must be named Hello.java, matching the public class. Statements end with semicolons, and braces mark blocks.
Compiling and running
javac Hello.java compiles the source into Hello.class bytecode; java Hello runs it on the JVM. Modern Java also allows running a single source file directly with java Hello.java. Integrated development environments such as IntelliJ IDEA and Eclipse compile automatically and highlight errors as you type.
Primitive types
Java has eight primitive types. The most used are int (32-bit whole numbers), long (64-bit whole numbers), double (64-bit decimals), boolean (true or false), and char (a single character in single quotes, such as 'A'). byte, short, and float complete the set. Integer division truncates: 7 / 2 is 3, while 7.0 / 2 is 3.5.
Declaring variables
Variables must be declared with a type: int count = 0; double price = 9.99;. Assigning the wrong type, such as putting text into an int, is a compile-time error. Since Java 10, var lets the compiler infer the type of a local variable: var total = 0; still makes total an int; it is not dynamic typing.
Strings
String is a class, not a primitive, so string values are objects. Use double quotes: String name = "Ana";. + concatenates strings. Compare string contents with equals, as in name.equals("Ana"), not ==, which checks whether two references point to the same object. Strings are immutable, like Python's.
Control flow
Java's if, else if, and else work as in Python but with conditions in parentheses and braces around blocks. Loops include for (int i = 0; i < 10; i++) { ... }, the enhanced for loop for (String s : names) { ... }, while, and do-while, which always runs at least once. switch selects among cases; modern switch expressions use arrows: case 1 -> "one";.
Worked example
public class Average { public static void main(String[] args) { int[] scores = {72, 85, 90}; int sum = 0; for (int s : scores) { sum += s; } double avg = (double) sum / scores.length; System.out.printf("%.2f%n", avg); } }. The cast (double) is essential: without it, 247 / 3 would use integer division and give 82, losing the fraction. With it, the program prints 82.33.
Key takeaways
Java (1995, Gosling, Sun) compiles to bytecode for the JVM: write once, run anywhere. Programs live in classes; main is the entry point; statements end with semicolons. javac compiles; java runs. Primitives include int, long, double, boolean, and char; integer division truncates. Variables must be declared with types; var infers them. Strings are immutable objects compared with equals. Control flow uses parentheses and braces; for, enhanced for, while, do-while, and switch are available.
Flashcard
Statically typed OOP language, 1995
Check your understanding
In Java, why should you compare strings with a.equals(b) rather than a == b?
