About the Java online compiler
Java is the backbone of enterprise software and Android, and it is what most Indian universities teach for object-oriented programming. This online Java compiler builds and runs your class without any JDK installation, so you can practise anywhere — including from a phone.
Everything lives inside a class with a public static void main(String[] args) entry point. From there you can practise inheritance, interfaces, collections such as ArrayList and HashMap, exception handling, and the Scanner class for reading input.
- →Completing college OOP practicals without a local JDK
- →Practising collections: ArrayList, HashMap, HashSet
- →Learning inheritance and interface implementation
- →Preparing for placement coding rounds
Java sample programs you can run right now
Copy any snippet into the editor above and press Run. Each one is short on purpose — retype it from memory afterwards.
Classes and inheritance
class Animal {
String name;
Animal(String name) { this.name = name; }
void speak() { System.out.println(name + " makes a sound"); }
}
class Dog extends Animal {
Dog(String name) { super(name); }
@Override void speak() { System.out.println(name + " barks"); }
}
public class Main {
public static void main(String[] args) {
new Dog("Bruno").speak();
}
}
The subclass overrides speak(); super(name) passes the argument up to the parent constructor.
Collections
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<String, Integer> marks = new HashMap<>();
marks.put("Aarav", 78);
marks.put("Priya", 91);
for (Map.Entry<String, Integer> e : marks.entrySet())
System.out.println(e.getKey() + " -> " + e.getValue());
}
}
HashMap has no guaranteed order — use LinkedHashMap when insertion order matters.
Common Java errors and how to fix them
error: class Main is public, should be declared in a file named Main.java
Why: The public class name does not match the file name the runner uses.
Fix: Name your public class Main.
cannot find symbol
Why: A variable, method or class name is unknown at that point.
Fix: Check spelling, declare the variable, or add the missing import (for example java.util.*).
Exception in thread "main" java.lang.NullPointerException
Why: You called a method on a reference that is null.
Fix: Initialise the object before use, or guard with an if (obj != null) check.
Java syntax cheatsheet
| Concept | Syntax |
|---|
| Entry point | public static void main(String[] args) |
| Print | System.out.println(x); |
| List | List<String> l = new ArrayList<>(); |
| For-each | for (String s : list) { } |
| Read input | Scanner sc = new Scanner(System.in); |
| Interface | class A implements B { } |
Java compiler FAQs
What should I name my class?
Name the public class Main — the compiler expects that entry point.
Can I read input with Scanner?
Yes. Enter the values in the standard input box below the editor, one per line.
Keep going after the compiler
Running snippets builds speed; the 17-chapter course builds understanding. Work through the Java chapters, take the quiz, then claim your certificate.
Other compilers: JavaScript compiler, TypeScript compiler, Python 3 compiler, C compiler, C++ compiler, Go compiler, Rust compiler, HTML compiler, CSS compiler, SQLite compiler