Java Code Templates

Hello World

Prints Hello World in Java console.

Hello World Preview
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Variables

Declare variables and print them.

Variables Preview
int x = 10;
int y = 5;
System.out.println(x + y);

If Statement

Simple if-else example.

If Statement Preview
int age = 18;
if(age >= 18){
    System.out.println("Adult");
}else{
    System.out.println("Minor");
}

For Loop

Loop through numbers 1 to 5.

For Loop Preview
for(int i=1; i<=5; i++){
    System.out.println(i);
}

While Loop

Loop until a condition is false.

While Loop Preview
int count = 0;
while(count < 5){
    System.out.println(count);
    count++;
}

Functions (Methods)

Define a method and call it.

Methods Preview
public static void greet(String name){
    System.out.println("Hello " + name);
}
greet("Alice");

Arrays

Create an array and loop through it.

Arrays Preview
int[] numbers = {1,2,3,4,5};
for(int num : numbers){
    System.out.println(num);
}

Strings

Manipulate strings in Java.

Strings Preview
String text = "Hello Java";
System.out.println(text.toUpperCase());

Scanner Input

Read input from user.

Scanner Preview
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello " + name);

Math Operations

Basic math operations in Java.

Math Preview
int a = 10, b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);

2D Array

Create and access 2D arrays.

2D Array Preview
int[][] matrix = {{1,2},{3,4}};
System.out.println(matrix[0][1]);

ArrayList

Use ArrayList to store elements.

ArrayList Preview
import java.util.ArrayList;
ArrayList list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list);

For Each Loop

Loop through ArrayList using for-each.

ForEach Preview
for(String fruit : list){
    System.out.println(fruit);
}

Switch Statement

Example of switch-case in Java.

Switch Preview
int day = 3;
switch(day){
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    case 3: System.out.println("Wednesday"); break;
}

Try-Catch

Handle exceptions in Java.

TryCatch Preview
try{
    int a = 5/0;
}catch(Exception e){
    System.out.println("Error: " + e);
}

Classes

Define a simple class and object.

Class Preview
class Person{
    String name;
    int age;
}
Person p = new Person();
p.name = "Alice";
p.age = 25;
System.out.println(p.name);

Static Method

Define and call a static method.

Static Method Preview
class Utils{
    static void greet(String name){
        System.out.println("Hello " + name);
    }
}
Utils.greet("Alice");

Final Variable

Use final keyword to make a constant.

Final Variable Preview
final int MAX = 100;
System.out.println(MAX);

Random Number

Generate a random number using Math.random().

Random Preview
int rand = (int)(Math.random()*10)+1;
System.out.println(rand);

Comments

Single-line and multi-line comments in Java.

Comments Preview
// This is a single-line comment
/* This is 
   a multi-line comment */