Java · Playground
Java playground
Running Java inside the browser is not available yet. Use this editor to write, then download the file and run it locally with OpenJDK 21. The example snippets were verified that way.
public class Main {
public static void main(String[] args) {
System.out.println("Corner Bakery opens at 7:00");
System.out.println("Today's special: rye loaf");
}
}Output
Press Run to see the program’s output here.
Running Java inside the browser is not available yet. Use this editor to write, then download the file and run it locally with OpenJDK 21. The example snippets were verified that way.
Java examples
Each one was run with OpenJDK 21 before it was published. Load one, change it, run it.
- Java · Java Syntax and Your First Program: The smallest useful program
public class Main { public static void main(String[] args) { System.out.println("Corner Bakery opens at 7:00"); System.out.println("Today's special: rye loaf"); } } - Java · Java Syntax and Your First Program: Comments, statements and layout
public class Main { // The program starts in main. public static void main(String[] args) { int shelves = 4; /* every statement ends with a semicolon */ int perShelf = 12; int capacity = shelves * perShelf; System.out.println("Shelf capacity: " + capacity); System.out.println( "Two lines of source," + " one statement" ); } } - Java · Variables and Data Types in Java: Declaring the everyday types
public class Main { public static void main(String[] args) { int seats = 42; long odometerKm = 3_250_000_000L; double fareEuro = 2.75; boolean electric = true; char route = 'B'; String depot = "North Yard"; System.out.println("Route " + route + " from " + depot); System.out.println("Seats: " + seats); System.out.println("Odometer: " + odometerKm + " km"); System.out.println("Fare: " + fareEuro); System.out.println("Electric: " + electric); } } - Java · Variables and Data Types in Java: Overflow, var and floating-point surprises
public class Main { public static void main(String[] args) { int big = 2_000_000_000; int wrapped = big + big; long safe = (long) big + big; System.out.println(wrapped); System.out.println(safe); System.out.println(Integer.MAX_VALUE); var label = "platform"; var count = 3; System.out.println(label + " " + count); System.out.println(0.1 + 0.2); } } - Java · Operators in Java: Comparison, logic and short-circuiting
public class Main { public static void main(String[] args) { int age = 17; boolean hasPass = true; int seatsLeft = 0; System.out.println(age >= 18); System.out.println(age >= 16 && hasPass); System.out.println(seatsLeft > 0 || hasPass); System.out.println(!hasPass); boolean ok = seatsLeft != 0 && 10 / seatsLeft > 1; System.out.println(ok); String label = age >= 18 ? "adult" : "minor"; System.out.println(label); } }
How this page was checked. Every program on it was run with OpenJDK 21 at build time by the publishing checks, and the output shown is what it printed. Running Java inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with OpenJDK 21 locally.