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
Java tutorial: five steps from blank editor to working program
These steps cover the Java you need for school, college and entry-level interviews: classes and main, typed variables, control flow, arrays and collections, objects, and exception handling.
1. Every program lives in a class
Java runs the static main method of the class you execute. The class name and file name normally match, and printing uses System.out.println.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
2. Declare the type of every variable
Java is statically typed: int, double, boolean, char and String each hold a specific kind of value, and the compiler rejects mismatches.
int marks = 87;
double percent = marks / 100.0;
boolean passed = marks >= 50;
System.out.println(percent + " " + passed);
3. Branch and loop
if/else, switch, for and while behave as in C-family languages. Integer division truncates, which is why the example above divides by 100.0.
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) System.out.println(i + " even");
else System.out.println(i + " odd");
}
4. Store many values with arrays and ArrayList
An array has a fixed length; an ArrayList grows as you add. Use the ArrayList for anything whose size you do not know upfront.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Aarav");
names.add("Diya");
for (String n : names) System.out.println(n);
}
}
5. Model things as objects
A class groups fields with methods. Keep fields private and expose behaviour through methods — that is encapsulation, the idea most Java interviews start from.
class Student {
private final String name;
private final int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
boolean passed() { return marks >= 50; }
String getName() { return name; }
}
public class Main {
public static void main(String[] args) {
Student s = new Student("Diya", 72);
System.out.println(s.getName() + " " + s.passed());
}
}
Practice exercises with solutions
Try each one in the editor above before opening the solution — the struggle is where the learning happens.
Beginner
Print the sum of the first 100 natural numbers using a loop.
Hint: Accumulate into an int declared before the loop.
Show solution
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
System.out.println(sum);
}
}
Intermediate
Reverse a string without using StringBuilder.reverse().
Hint: Walk the characters from the last index down to 0.
Show solution
public class Main {
public static void main(String[] args) {
String s = "coding";
String out = "";
for (int i = s.length() - 1; i >= 0; i--) out += s.charAt(i);
System.out.println(out);
}
}
Advanced
Count word frequencies in a sentence using a HashMap.
Hint: split(" ") then merge with getOrDefault.
Show solution
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
String text = "code play code learn";
HashMap<String, Integer> freq = new HashMap<>();
for (String w : text.split(" "))
freq.put(w, freq.getOrDefault(w, 0) + 1);
System.out.println(freq);
}
}
Why learn Java?
Java is the language most Indian universities and technical interviews use to teach and test object-oriented programming and data structures.
It runs on the JVM, so the same compiled program works on Windows, macOS, Linux and Android without changes.
What Java is used for
- →Android app development
- →Enterprise backends with Spring Boot
- →Data-structures and algorithm practice for interviews
- →Large-scale systems in banking and telecom
Ready for structured practice? The 17-chapter Java course takes these ideas one at a time, with a quiz and a certificate at the end.