OpenJDK 22 (Java 22.0.2) + javacIsolated Sandbox

Online Java Compiler — Write, Compile & Run Java Online

Veyrai's Online Java Compiler runs on an isolated OpenJDK 22 cloud environment. It gives engineers and students an instant, zero-setup browser IDE to write Java code, experiment with algorithms, parse standard input with Scanner or BufferedReader, and verify object-oriented design patterns without configuring local JDKs or CLASSPATH variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Execution Output
Click "Run (Ctrl+Enter)" to compile and execute your Java program in the isolated sandbox.

How to Use the Online Java Compiler

1

Keep the Main Class

Java requires a main entry point. In our cloud sandbox, define your entry class as 'public class Main' containing 'public static void main(String[] args)'.

2

Provide Standard Input (Optional)

If your code reads from System.in using Scanner or BufferedReader, click 'Standard Input (stdin)' to supply inputs separated by spaces or newlines before clicking Run.

3

Click Run Code

The cloud sandbox compiles your code with javac and executes bytecode in an isolated container. Real stdout, runtime latency, and exit codes appear in the console below.

4

Practice in AI Technical Mock Interviews

Once your solution works, take it to Veyrai's AI Interviewer to explain your time/space complexity and defend your architectural choices out loud.

Java Code Examples & Algorithms

Each example is verified to run in our production OpenJDK 22 (Java 22.0.2) + javac runtime. Click "Load in Editor" to test and modify any example above.

1. Standard Input & Scanner Parsing

Read integers and strings dynamically from stdin using java.util.Scanner.

Time: O(1)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNext()) {
            String name = scanner.next();
            int age = scanner.hasNextInt() ? scanner.nextInt() : 0;
            System.out.println("Candidate Name: " + name);
            System.out.println("Candidate Age:  " + age);
            System.out.println("Eligibility:    " + (age >= 18 ? "Eligible" : "Minor"));
        } else {
            System.out.println("Please provide stdin inputs in the input drawer.");
        }
    }
}

2. Two Sum Problem (HashMap Optimization)

Find two numbers in an array that sum to a target value in O(N) linear time using java.util.HashMap.

Time: O(N)
import java.util.HashMap;
import java.util.Map;
import java.util.Arrays;

public class Main {
    public static int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> numMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (numMap.containsKey(complement)) {
                return new int[] { numMap.get(complement), i };
            }
            numMap.put(nums[i], i);
        }
        return new int[] {};
    }

    public static void main(String[] args) {
        int[] nums = { 2, 7, 11, 15 };
        int target = 9;
        int[] result = twoSum(nums, target);
        System.out.println("Array:  " + Arrays.toString(nums));
        System.out.println("Target: " + target);
        System.out.println("Indices Found: " + Arrays.toString(result));
    }
}

3. Iterative Binary Search

Classic logarithmic search over a sorted array with overflow-safe midpoint calculation.

Time: O(log N)
public class Main {
    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            // Prevent integer overflow: left + (right - left) / 2
            int mid = left + (right - left) / 2;

            if (arr[mid] == target) {
                return mid;
            }
            if (arr[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] sortedData = { 3, 9, 14, 19, 27, 33, 42, 51, 68 };
        int target = 33;
        int index = binarySearch(sortedData, target);
        System.out.println("Target: " + target + " found at index: " + index);
    }
}

Common Java Beginner Mistakes & Fixes

Class Name Mismatch ('class Main' vs another name)

Java compilers enforce file/class naming conventions. In our online sandbox, your public class must be named 'Main' to match the execution runner.

Fix: Rename your class to 'public class Main' with the standard static main method signature.
Integer Overflow in Midpoint Calculation ('(low + high) / 2')

If low + high exceeds Integer.MAX_VALUE (2,147,483,647), the sum rolls over into negative values, causing an ArrayIndexOutOfBoundsException.

Fix: Always write midpoint calculations as 'int mid = low + (high - low) / 2;'.
Comparing Objects with '==' instead of '.equals()'

Using '==' on Java Objects (such as Strings or custom objects) compares reference equality (memory addresses) rather than content equality.

Fix: Use 'str1.equals(str2)' for value comparison, or 'Objects.equals(a, b)' to safely guard against NullPointerException.

Environment Specifications

Language
Java
Compiler / Engine
OpenJDK 22 (Java 22.0.2) + javac
Standard Input
Supported (stdin)
Isolation Layer
Dockerized Sandbox
Execution Timeout
8s to 25s max
Guest Access
Allowed (Rate limited)

Ready to Defend Your Code?

Coding is only 50% of a real technical round. Can you defend your Big-O complexity, explain memory trade-offs, and handle follow-up curveballs out loud?

Launch AI Technical Mock
Recommended Reading

Top 25 Java Interview Questions & Answers

Curated technical questions, code walkthroughs, edge cases, and follow-ups asked in top Indian tech companies and global MNCs.

Read Interview Guide
Online Java Compiler – Write and Run Java Code | Veyrai | Veyrai