You’re the Expert!

java

Cheatsheets

Print "Hello, World!" to the Console
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
copy to clipboard
Declare and Use Variables
public class VariablesExample {
    public static void main(String[] args) {
        String name = "Santosh";
        int age = 25;
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}
copy to clipboard
Perform Arithmetic Operations
public class ArithmeticOperations {
    public static void main(String[] args) {
        int a = 10, b = 5;
        System.out.println("Addition: " + (a + b));
        System.out.println("Subtraction: " + (a - b));
        System.out.println("Multiplication: " + (a * b));
        System.out.println("Division: " + (a / b));
        System.out.println("Modulus: " + (a % b));
    }
}
copy to clipboard
Conditional Statements (if-else)
public class ConditionalExample {
    public static void main(String[] args) {
        int age = 20;
        if (age >= 18) {
            System.out.println("Eligible for voting");
        } else {
            System.out.println("Not eligible for voting");
        }
    }
}
copy to clipboard
Using Loops (for, while)
public class LoopExample {
    public static void main(String[] args) {
        // For loop example
        for (int i = 1; i <= 5; i++) {
            System.out.println("Santosh is learning Java - Iteration: " + i);
        }

        // While loop example
        int count = 1;
        while (count <= 3) {
            System.out.println("Dhruv is exploring Java - Attempt: " + count);
            count++;
        }
    }
}
copy to clipboard
Arrays in Java
public class ArrayExample {
    public static void main(String[] args) {
        String[] names = {"Santosh", "Kumar", "Dhruv", "Pynfinity"};
        for (String name : names) {
            System.out.println("Name: " + name);
        }
    }
}
copy to clipboard

Methods in Java
public class MethodExample {
    public static void greet(String name) {
        System.out.println("Hello, " + name + "! Welcome to Pynfinity channel.");
    }

    public static void main(String[] args) {
        greet("Santosh");
        greet("Kumar");
    }
}
copy to clipboard
Classes and Objects
public class Person {
    String name;
    int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display information
    public void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }

    public static void main(String[] args) {
        Person person1 = new Person("Dhruv", 25);
        person1.displayInfo();
    }
}
copy to clipboard
Using Switch-Case
public class SwitchExample {
    public static void main(String[] args) {
        String day = "Tuesday";
        switch (day) {
            case "Monday":
                System.out.println("Start of the week!");
                break;
            case "Tuesday":
                System.out.println("Second day of the week.");
                break;
            case "Wednesday":
                System.out.println("Mid-week day.");
                break;
            default:
                System.out.println("Not a valid day.");
                break;
        }
    }
}
copy to clipboard
Working with Strings
public class StringExample {
    public static void main(String[] args) {
        String channelName = "Pynfinity";
        String message = "Welcome to the " + channelName + " educational channel!";

        // Length of the string
        System.out.println("Message length: " + message.length());

        // Convert to uppercase
        System.out.println("Uppercase: " + message.toUpperCase());

        // Check if the string contains a specific word
        System.out.println("Contains 'educational': " + message.contains("educational"));
    }
}
copy to clipboard
Exception Handling (try-catch)
public class ExceptionExample {
    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Error: Division by zero is not allowed.");
        }
    }
}
copy to clipboard

Inheritance in Java
class Animal {
    void sound() {
        System.out.println("Animals make sounds");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dogs bark");
    }
}

public class InheritanceExample {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound();  // Dog's sound
    }
}
copy to clipboard
Polymorphism in Java
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        System.out.println("Dog barks");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("Cat meows");
    }
}

public class PolymorphismExample {
    public static void main(String[] args) {
        Animal myAnimal = new Animal();
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myAnimal.sound();
        myDog.sound();
        myCat.sound();
    }
}
copy to clipboard
Abstract Classes in Java
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class AbstractClassExample {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound();  // Dog's sound
    }
}
copy to clipboard
Interfaces in Java
interface Animal {
    void sound();
}

class Dog implements Animal {
    public void sound() {
        System.out.println("Dog barks");
    }
}

public class InterfaceExample {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.sound();  // Dog's sound
    }
}
copy to clipboard
Working with Collections (ArrayList)
import java.util.ArrayList;

public class CollectionExample {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        names.add("Santosh");
        names.add("Kumar");
        names.add("Dhruv");

        // Print all elements
        for (String name : names) {
            System.out.println("Name: " + name);
        }
    }
}
copy to clipboard
File Handling in Java
import java.io.*;

public class FileHandlingExample {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("example.txt");
            writer.write("Hello, this is a test file for Pynfinity educational channel.");
            writer.close();

            FileReader reader = new FileReader("example.txt");
            int i;
            while ((i = reader.read()) != -1) {
                System.out.print((char) i);
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}
copy to clipboard