Java
Java is a programming language widely used to build backend services. Its compiler checks types before the program runs, and the compiled code typically runs on the Java Virtual Machine (JVM).
What is Java?
Java is a statically typed programming language. The compiler checks that the values you use match the types an operation expects. If a method takes an int, passing the string "42" is a compile-time error unless you convert it first. Java is also class-based: classes define how objects store data and behave, while interfaces let different classes offer the same operations. Not every value is an object, though. Types such as int and boolean are primitives.
From source file to running program
The Java Development Kit (JDK) provides development tools and a runtime. Its javac compiler turns .java source files into .class files containing bytecode. The Java Virtual Machine (JVM) loads those classes and runs their bytecode. A JVM such as HotSpot can interpret instructions at first and compile frequently executed code into native machine instructions as the program runs. This is called just-in-time (JIT) compilation.
Bytecode is portable across operating systems when a compatible JVM and the required dependencies are available. Portability doesn't mean platform differences disappear: an application might still use a native library built for a specific operating system.
Example: validating input
public class PageSize {
static int parsePageSize(String input) {
int size = Integer.parseInt(input);
if (size < 1 || size > 100) {
throw new IllegalArgumentException("Expected 1 to 100");
}
return size;
}
public static void main(String[] args) {
System.out.println(parsePageSize("25"));
}
}
Save the example as PageSize.java, run javac PageSize.java, and then run java PageSize. It prints 25. The method takes text, like a value from an HTTP query parameter, and turns it into an integer. The compiler checks that the argument is a String, but only at runtime can the program tell whether that string contains a valid number and whether the number is in range.
Where Java fits in the backend
Teams use Java to build HTTP APIs, process messages, and run scheduled jobs. Spring is a framework often used for these applications, not a feature of the language. Likewise, the JVM is not exclusive to Java: Kotlin can target it too. Java and JavaScript are separate languages despite their names.
Java's garbage collector reclaims memory from objects the program can no longer reach. It won't reclaim an object that an ever-growing cache still references, even if the application never reads that entry again. Garbage collection is also not a substitute for closing resources such as files and database connections.
What interviewers look for
Expect to explain what happens between compiling and launching a program, then reason through a small snippet. Common checks include string equality and parameter passing. When two variables refer to the same object, distinguish changing that object's state from assigning a different reference to one variable.
“Java” questions from real interviews
Asked at 238 companies