// Online Java compiler to compile and run Java code in the browser.
// Write Java code in this online editor — no JDK install needed.
public class Main {
public static void main(String[] args) {
Greeter g = new Greeter();
g.greet();
}
}
class Greeter {
void greet() {
System.out.println("Welcome to Tutorsbot!");
}
}
Paste a Java program, press Run, and the compiler and the JVM do the rest. You can keep several classes in one file, use the Collections and Streams APIs, and read the same compile errors and exception traces javac and java print on your own machine.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner("3\nAsha 91\nRavi 78\nMeena 85");
int n = sc.nextInt();
Student best = null;
for (int i = 0; i < n; i++) {
Student s = new Student(sc.next(), sc.nextInt());
if (best == null || s.marks > best.marks) best = s;
}
System.out.println("Top: " + best.name + " (" + best.marks + ")");
}
}
class Student {
String name;
int marks;
Student(String name, int marks) { this.name = name; this.marks = marks; }
}
Start with a class that has public static void main(String[] args) and put it first in the file. Java runs the first class it finds, so if a helper class such as Student comes before Main, the run stops with can't find main(String[]) method in class: Student. The class does not have to be called Main: public class Hello works the same way.
Below that first class you can add as many other classes, interfaces and enums as you like, without the public keyword. That covers most programs from a Java course or an interview: a model class, a service class and a main that ties them together. Import whatever you need from the JDK, such as java.util.*, java.util.stream.*, java.time.* or java.util.concurrent.*.
A run gets no keyboard input, so new Scanner(System.in) followed by nextInt() throws java.util.NoSuchElementException. The fix keeps the rest of your code unchanged: give the Scanner a string instead of System.in.
Scanner sc = new Scanner("3\nAsha 91\nRavi 78\nMeena 85");nextInt(), next() and nextLine() read that text exactly as they would read what a user typed. Each \n marks a new line. This is also the fastest way to test a HackerRank or LeetCode solution against the sample input. Switch back to System.in before you submit.
The everyday features of modern Java all compile here: var for local variables, lambdas and method references, the Streams API with Collectors.groupingBy, multi-line text blocks written between """ marks, and switch expressions such as case 1 -> "Mon";.
Records do not. record Point(int x, int y) {} fails to compile. Write a small class with final fields, a constructor and a toString() instead, which is also what many interviewers still expect you to be able to write by hand.
GUI code is out of scope as well. Swing, AWT and JavaFX need a screen to draw on, and there is none, so practise GUI programs in an IDE such as IntelliJ IDEA, Eclipse or BlueJ.
Java reports problems in two stages, and the output tells you which one you hit.
file0.code.java:7: error: ';' expected, followed by the line with a caret under the problem. Common ones: a missing semicolon, a typo in a variable name (cannot find symbol), and returning the wrong type (incompatible types).ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3, and the line at Main.main(file0.code.java:4) tells you where.Practising exception handling? Wrap the risky call in try/catch, print e.getMessage(), and add a finally block to see that it always runs.
Java gets more time than most languages here, because starting the JVM is part of every run. A program has 12 seconds to finish and up to 45 seconds to compile, and a typical small program completes in under two seconds. Other limits:
java.nio.file.Files can be read back during the same run and are then removed.Most Java interview rounds test collections and OOP more than syntax. These are good programs to write and run here:
HashMap, then do it again with Collectors.groupingBy and a TreeMap so the keys come out sorted.ArrayDeque and use it to check whether brackets in a string are balanced.equals and hashCode in a class, put two equal objects in a HashSet, and print its size. Then remove hashCode and see what changes.Comparator.comparing, then add .thenComparing for a tie-breaker.ExecutorService, collect the results with Future.get(), and shut the pool down.On the compiler or interpreter question: javac compiles your source to bytecode, and the JVM interprets that bytecode and compiles the hot parts to machine code while the program runs. A compile error is javac's; an exception is the JVM's.