Online Java Compiler – Run and Test Java Code in Your Browser

Write, compile, and run Java code instantly in your browser with our free online Java compiler. No installation needed β€” ideal for learning, testing, and quick prototyping.

πŸš€ 2 total executions (2 this month)

πŸ“š The best Java courses you should try

Loading...

πŸ’‘ Java Basics Guide for Beginners

1. Declaring Variables and Constants

Java requires you to declare the type of each variable. Use final for constants.

int age = 30;
double pi = 3.14159;
char grade = 'A';
String name = "Alice";
boolean isActive = true;

// Constants
final int MAX_USERS = 100;
final String COMPANY = "CodeUtility";

2. Conditionals (if / switch)

Use if, else if, and switch for control flow.

int x = 2;
if (x == 1) {
    System.out.println("One");
} else if (x == 2) {
    System.out.println("Two");
} else {
    System.out.println("Other");
}

switch (x) {
    case 1:
        System.out.println("One");
        break;
    case 2:
        System.out.println("Two");
        break;
    default:
        System.out.println("Other");
}

3. Loops

Use for, while, and do-while for iteration.

for (int i = 0; i < 3; i++) {
    System.out.println(i);
}

int n = 3;
while (n > 0) {
    System.out.println(n);
    n--;
}

4. Arrays

Arrays hold fixed-size sequences of the same type.

int[] numbers = {10, 20, 30};
System.out.println(numbers[1]);

5. ArrayList Manipulation

Use ArrayList for dynamic-sized lists.

import java.util.ArrayList;

ArrayList<Integer> nums = new ArrayList<>();
nums.add(1);
nums.add(2);
nums.add(3);
nums.remove(Integer.valueOf(2));

for (int num : nums) {
    System.out.print(num + " ");
}

6. Console Input/Output

Use Scanner for input and System.out for output.

import java.util.Scanner;

Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);

7. Functions

Define methods with return types and parameters.

public static int add(int a, int b) {
    return a + b;
}

System.out.println(add(3, 4));

8. HashMaps

Use HashMap for key-value storage.

import java.util.HashMap;

HashMap<String, Integer> ages = new HashMap<>();
ages.put("Alice", 30);
System.out.println(ages.get("Alice"));

9. Exception Handling

Use try and catch to handle runtime exceptions.

try {
    throw new Exception("Something went wrong");
} catch (Exception e) {
    System.out.println(e.getMessage());
}

10. File I/O

Use Files and Paths from java.nio.file for file handling.

import java.nio.file.*;
import java.io.IOException;

Files.writeString(Paths.get("file.txt"), "Hello File");
String content = Files.readString(Paths.get("file.txt"));
System.out.println(content);

11. String Manipulation

Java strings support many methods like length(), substring(), and contains().

String text = "Hello World";
System.out.println(text.length());
System.out.println(text.substring(0, 5));
System.out.println(text.contains("World"));

12. Classes & Objects

Java supports object-oriented programming using classes and instances.

class Person {
    String name;
    Person(String name) {
        this.name = name;
    }
    void greet() {
        System.out.println("Hi, I'm " + name);
    }
}

Person p = new Person("Alice");
p.greet();