Online Java Compiler and Runner Core Java java Java 21 by devs5003 - May 31, 2025June 16, 20250 Last Updated on June 16th, 2025 Looking for a quick way to compile and execute Java code online without installing JDK? Our free Online Java Compiler lets you write, run, and debug Java programs instantly in your browser: supports Java 21 (latest LTS version)! 👨💻 Try It Now: Online Java Compiler Below Enter your Java code: Run Code Result: 📢 Bookmark this page for quick access whenever you need to test Java code! Table of Contents Toggle Why Use This Online Java Compiler? How to Use the Online Java Compiler?Examples to Get You Started1. Hello World:2. Java 21 Record Example:3. Check if a Number is Prime4. Fibonacci Series up to N Terms5. Palindrome String Check6. Factorial of a Number7. Armstrong Number Check8. Reverse a Number9. Check Even or Odd10. Find Largest Among Three Numbers11. Simple Calculator using Switch CaseExamples Using sealed classes, records, and pattern matching for switch: Java 21Example#1: Vehicle Toll Fee CalculatorOutput:Example#2: Payment Processing SystemOutput:Example#3: Employee Bonus CalculationOutput:Who Can Benefit From This Tool?Frequently Asked Questions (FAQs)Q#1. Can I use Java 21 features here?Q#2. Is this tool free to use?Q#3. Is my code stored?Conclusion Why Use This Online Java Compiler? ✅ No Installation RequiredYou don’t need to download JDK or set up IDEs like Eclipse or IntelliJ. Just open your browser and start coding! ✅ Supports Latest Java Version (Java 21)Write and test code with the newest Java features like: Pattern Matching for switch Record Classes, Sealed Classes/Interfaces Virtual Threads (Project Loom) ✅ Beginner-Friendly InterfaceThe simple and clean interface is perfect for learning and quick testing. ✅ Fast Compilation & ExecutionGet results within seconds with a modern backend optimized for speed. ✅ Safe & Secure ExecutionAll code is sandboxed, ensuring security and preventing misuse. How to Use the Online Java Compiler? Type or paste your Java code in the editor Click “Run” to compile & execute View output instantly in the console Examples to Get You Started 1. Hello World: public class Main { public static void main(String[] args) { System.out.println("Hello, Java!"); } } 2. Java 21 Record Example: record Person(String name, int age) {} public class Main { public static void main(String[] args) { Person p = new Person("Alice", 30); System.out.println(p); } } 3. Check if a Number is Prime Purpose: Determine if a number is prime or not. public class PrimeCheck { public static void main(String[] args) { int num = 29; boolean isPrime = true; for (int i = 2; i <= num / 2; i++) { if (num % i == 0) { isPrime = false; break; } } System.out.println(num + (isPrime ? " is a prime number." : " is not a prime number.")); } } 4. Fibonacci Series up to N Terms Purpose: Print the Fibonacci sequence up to a specified number of terms. public class FibonacciSeries { public static void main(String[] args) { int n = 10, a = 0, b = 1; System.out.print("Fibonacci Series: " + a + ", " + b); for (int i = 2; i < n; i++) { int next = a + b; System.out.print(", " + next); a = b; b = next; } } } 5. Palindrome String Check Purpose: Check if a string is a palindrome (reads the same forward and backward). public class PalindromeCheck { public static void main(String[] args) { String str = "madam"; String reversed = new StringBuilder(str).reverse().toString(); if (str.equals(reversed)) { System.out.println(str + " is a palindrome."); } else { System.out.println(str + " is not a palindrome."); } } } 6. Factorial of a Number Purpose: Calculate the factorial of a given number using recursion. public class Factorial { static int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } public static void main(String[] args) { int num = 5; System.out.println("Factorial of " + num + " is: " + factorial(num)); } } 7. Armstrong Number Check Purpose: Check whether a number is an Armstrong number (e.g., 153). public class ArmstrongNumber { public static void main(String[] args) { int num = 153, original = num, result = 0; while (original != 0) { int digit = original % 10; result += Math.pow(digit, 3); original /= 10; } System.out.println(num + (result == num ? " is an Armstrong number." : " is not an Armstrong number.")); } } 8. Reverse a Number Purpose: Reverse the digits of an integer. public class ReverseNumber { public static void main(String[] args) { int num = 12345, reversed = 0; while (num != 0) { int digit = num % 10; reversed = reversed * 10 + digit; num /= 10; } System.out.println("Reversed number: " + reversed); } } 9. Check Even or Odd Purpose: Check if a number is even or odd. public class EvenOdd { public static void main(String[] args) { int num = 42; System.out.println(num + " is " + (num % 2 == 0 ? "Even" : "Odd")); } } 10. Find Largest Among Three Numbers Purpose: Use conditional statements to determine the largest number. public class LargestOfThree { public static void main(String[] args) { int a = 10, b = 25, c = 20; int largest; if (a >= b && a >= c) largest = a; else if (b >= a && b >= c) largest = b; else largest = c; System.out.println("Largest number is: " + largest); } } 11. Simple Calculator using Switch Case Purpose: Build a basic calculator using a switch statement. public class Calculator { public static void main(String[] args) { char operator = '+'; double num1 = 10, num2 = 5, result = 0; switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num2 != 0 ? num1 / num2 : 0; break; default: System.out.println("Invalid operator"); } System.out.println("Result: " + result); } } Examples Using sealed classes, records, and pattern matching for switch: Java 21 Below are real-world scenario examples combining pattern matching for switch and Java records, fully supported as preview features in Java 21. Each includes: The use of sealed interfaces and record types Pattern matching inside switch expressions Sample output to demonstrate real-world use cases You may also go through a separate detailed article on Java 21 Features with examples. Example#1: Vehicle Toll Fee Calculator You run a toll system that charges based on the type of vehicle. Each vehicle has different properties. sealed interface Vehicle permits Car, Truck, Motorcycle {} record Car(int passengers) implements Vehicle {} record Truck(double loadCapacityTons) implements Vehicle {} record Motorcycle(boolean hasSidecar) implements Vehicle {} public class TollCalculator { public static void main(String[] args) { Vehicle vehicle = new Truck(12.5); String feeMessage = switch (vehicle) { case Car c -> "Toll: $" + (5 + c.passengers()); case Truck t -> "Toll: $" + (15 + t.loadCapacityTons()); case Motorcycle m -> m.hasSidecar() ? "Toll: $4" : "Toll: $2"; }; System.out.println(feeMessage); } } Output: Toll: $27.5 Example#2: Payment Processing System You are building a payment system that handles different payment types. sealed interface Payment permits CreditCard, UPI, Cash {} record CreditCard(String cardNumber, double amount) implements Payment {} record UPI(String upiId, double amount) implements Payment {} record Cash(double amount) implements Payment {} public class PaymentProcessor { public static void main(String[] args) { Payment payment = new UPI("user@upi", 500.0); String result = switch (payment) { case CreditCard cc -> "Processing Credit Card: " + cc.cardNumber() + " for $" + cc.amount(); case UPI u -> "Processing UPI: " + u.upiId() + " for $" + u.amount(); case Cash c -> "Processing Cash Payment of $" + c.amount(); }; System.out.println(result); } } Output: Processing UPI: user@upi for $500.0 Example#3: Employee Bonus Calculation An HR system gives bonuses to employees based on their roles. sealed interface Employee permits Developer, Manager, Intern {} record Developer(String name, int completedProjects) implements Employee {} record Manager(String name, int teamSize) implements Employee {} record Intern(String name, boolean isPaid) implements Employee {} public class BonusCalculator { public static void main(String[] args) { Employee emp = new Developer("Alice", 6); String bonus = switch (emp) { case Developer d -> d.completedProjects() > 5 ? "Bonus for " + d.name() + ": $1000" : "Bonus for " + d.name() + ": $500"; case Manager m -> "Bonus for " + m.name() + ": $" + (m.teamSize() * 100); case Intern i -> i.isPaid() ? "Bonus for " + i.name() + ": $100" : "Bonus for " + i.name() + ": $0"; }; System.out.println(bonus); } } Output: Bonus for Alice: $1000 📝 All of these examples can be copied, modified, and executed directly in our Online Java Compiler. Simply paste the code, click Run, and see the result instantly. Who Can Benefit From This Tool? 🎓 Students practicing assignments 👩💻 Developers testing snippets 🧪 Educators demonstrating live Java code 🤖 Interview Candidates preparing coding rounds Frequently Asked Questions (FAQs) Q#1. Can I use Java 21 features here? Yes! Our compiler supports the latest Java 21 features. Q#2. Is this tool free to use? Absolutely. It’s completely free and doesn’t require registration. Q#3. Is my code stored? No. All code runs in memory and is discarded after execution. Conclusion The Online Java Compiler on this blog is a powerful yet easy-to-use tool designed to help Java enthusiasts write, test, and learn faster. Whether you’re exploring Java basics or Java 21 features, this platform will save you time and boost your productivity. For the release notes of all updated Java versions, kindly visit JDK Release Notes. Related