Back to home
java programming language icon

Java Online Compiler

Practice Java right here — no installs, no signups.

Java Compiler
Output
Code runs on the Play with Coding execution engine — your code is saved locally for next time.

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

ConceptSyntax
Entry pointpublic static void main(String[] args)
PrintSystem.out.println(x);
ListList<String> l = new ArrayList<>();
For-eachfor (String s : list) { }
Read inputScanner sc = new Scanner(System.in);
Interfaceclass 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