Online Java Compiler
Write, compile, and run Java code in the browser—great for learning, practice, and quick experiments.
Java Code Editor
Standard Input (optional)
Program Arguments (optional)
Output
Execution Info
Status & Timing:
Standard Output (stdout)
Ready. Program output will appear here.
Standard Error (stderr)
(no errors)
Usage
- Write a single-file Java program with a public static void main(String[] args) entry method.
- Need input? Put it in the Standard Input box; it will be passed to the program via stdin.
- Program arguments are space-separated. For example: hello world becomes args[0]=hello and args[1]=world.
- Click "Run Code" to compile and execute online.
- Timeouts: compilation and execution each have a 2000ms limit; keep programs simple.
- Restrictions: no network access; runs in a sandbox. Long-running or interactive programs may be terminated.
- Tip: use Scanner for input and System.out.println for output. Load the examples below to get started quickly.
Examples
Hello, World
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
} Read from stdin
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int n = sc.nextInt();
System.out.println("Name: " + name);
System.out.println("Twice: " + (2*n));
}
} Try standard input: Alice\n7
Program Arguments
public class Main {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("arg[" + i + "] = " + args[i]);
}
}
} Try arguments: hello world
Class and Method
class Greeter {
String greet(String name) {
return "Hello, " + name + "!";
}
}
public class Main {
public static void main(String[] args) {
Greeter g = new Greeter();
System.out.println(g.greet("Java"));
}
} Java Basics
Variables and Types
Java has primitive types (byte, short, int, long, float, double, char, boolean) and reference types (classes, arrays). Since Java 10, var can be used for local type inference.
public class Main {
public static void main(String[] args) {
byte b = 1;
int i = 42;
long l = 10000000000L;
float f = 3.14f;
double d = 2.71828;
char c = 'A';
boolean ok = true;
var s = "Java"; // type inference
System.out.println(b + " " + i + " " + l);
System.out.println(f + " " + d);
System.out.println(c + " " + ok + " " + s);
}
} Control Flow
Use if/else, switch, for, and while to control program flow. break and continue can alter loop execution.
public class Main {
public static void main(String[] args) {
int n = 7;
if (n % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
String day = "MON";
switch (day) {
case "MON":
case "TUE":
case "WED":
case "THU":
case "FRI":
System.out.println("weekday");
break;
case "SAT":
case "SUN":
System.out.println("weekend");
break;
default:
System.out.println("unknown");
}
for (int i = 0; i < 3; i++) System.out.println("i=" + i);
int j = 3;
while (j > 0) { System.out.println("j=" + j); j--; }
}
} Collections (List, Map)
The Collections Framework provides dynamic containers such as List and Map. Use enhanced for-loops to iterate.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("a"); list.add("b"); list.add("c");
for (String s : list) System.out.println(s);
Map<String,Integer> map = new HashMap<>();
map.put("alice", 3); map.put("bob", 5);
System.out.println("bob=" + map.get("bob"));
for (Map.Entry<String,Integer> e : map.entrySet()) {
System.out.println(e.getKey() + ":" + e.getValue());
}
}
} Exceptions (try/catch/finally)
Handle exceptions with try/catch/finally. Prefer catching specific exceptions; use finally for cleanup.
public class Main {
static int parse(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
System.out.println("Not a number: " + s);
return -1;
} finally {
System.out.println("Done parsing: " + s);
}
}
public static void main(String[] args) {
int a = parse("42");
int b = parse("hello");
System.out.println("a=" + a + ", b=" + b);
}
} Strings
Strings are immutable. Common operations include concatenation, case conversion, substring extraction, replace, and contains checks.
public class Main {
public static void main(String[] args) {
String s = "Java";
String t = "Spring";
String u = s + " " + t;
System.out.println(u.toUpperCase());
System.out.println(u.toLowerCase());
System.out.println(u.substring(0, 4));
System.out.println(u.replace("Spring", "World"));
System.out.println(u.contains("Java"));
}
}