Getting Started
5 snippetsYour first steps with Java basics
Hello World
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Comments
// Single line comment
/* Multi-line
comment */
/** Javadoc comment */User Input
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();Type Conversion
Integer.parseInt("42") // String to int
String.valueOf(42) // int to String
Double.parseDouble("3.14")Check Type
obj instanceof String // true/false
obj.getClass().getName()Variables & Data Types
8 snippetsDeclaring and using different data types
String
String name = "John";Integer
int age = 25;Double
double price = 19.99;Boolean
boolean isActive = true;Array
int[] nums = {1, 2, 3, 4, 5};ArrayList
ArrayList<String> list = new ArrayList<>();HashMap
HashMap<String, Integer> map = new HashMap<>();Final
final double PI = 3.14159;Operators
7 snippetsSymbols for calculations and comparisons
Arithmetic
5 + 3 // 8 (addition)
5 - 3 // 2 (subtraction)
5 * 3 // 15 (multiplication)
5 / 3 // 1 (integer division)
5 % 3 // 2 (modulus)
int x = 5; x++ // 6 (increment)Comparison
5 == 5 // true (equal)
5 != 3 // true (not equal)
5 > 3 // true (greater)
5 < 3 // false (less)
5 >= 5 // true
"a".equals("a") // true (strings)Logical
true && false // false (AND)
true || false // true (OR)
!true // false (NOT)
// Short-circuit: right side not evaluated
x != 0 && 10/x > 1Assignment
int x = 5 // Assign
x += 3 // x = 8 (add)
x -= 2 // x = 6 (subtract)
x *= 2 // x = 12 (multiply)
x /= 3 // x = 4 (divide)
x %= 3 // x = 1 (modulus)Ternary
int a = 10, b = 5;
int max = (a > b) ? a : b; // max = 10Instanceof
Object obj = "Hello";
if (obj instanceof String s) {
System.out.println(s.length()); // 5
}Bitwise
5 & 3 // 1 (AND: 101 & 011)
5 | 3 // 7 (OR: 101 | 011)
5 ^ 3 // 6 (XOR)
~5 // -6 (NOT)
5 << 1 // 10 (left shift)
5 >> 1 // 2 (right shift)Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Loops
10 snippetsRepeating code blocks efficiently
For
for (int i = 0; i < 5; i++) {
System.out.println(i);
}Enhanced For
for (String item : list) {
System.out.println(item);
}While
while (count < 10) {
count++;
}Do While
do {
count++;
} while (count < 10);Break
for (int i = 0; i < 10; i++) {
if (i == 5) break;
}Continue
for (int i = 0; i < 10; i++) {
if (i == 5) continue;
System.out.println(i);
}Labeled Break
outer: for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) break outer;
}
}forEach (Streams)
list.forEach(item -> {
System.out.println(item);
});Iterator
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}Map Iteration
map.forEach((key, value) -> {
System.out.println(key + ": " + value);
});Conditionals
5 snippetsMaking decisions in your code
If/Else
if (x > 0) {
System.out.println("positive");
} else if (x < 0) {
System.out.println("negative");
} else {
System.out.println("zero");
}Switch Expression
String result = switch (day) {
case "Mon" -> "Monday";
case "Tue" -> "Tuesday";
default -> "Other";
};Switch Statement
switch (day) {
case "Mon":
System.out.println("Monday");
break;
default:
System.out.println("Other");
}Pattern Matching
if (obj instanceof String s && s.length() > 5) {
System.out.println(s);
}Ternary
String msg = (age >= 18) ? "adult" : "minor";Methods
4 snippetsMethod
public String greet(String name) {
return "Hello, " + name + "!";
}Static Method
public static int add(int a, int b) {
return a + b;
}Void Method
public void printMessage(String msg) {
System.out.println(msg);
}Varargs
public int sum(int... nums) {
return Arrays.stream(nums).sum();
}Classes & OOP
4 snippetsClass
public class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String bark() {
return name + " says woof!";
}
}Inheritance
public class Puppy extends Dog {
public Puppy(String name) {
super(name);
}
}Interface
public interface Animal {
void speak();
}Record
public record Person(String name, int age) {}Exception Handling
3 snippetsTry/Catch
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}Finally
try {
riskyOperation();
} catch (Exception e) {
e.printStackTrace();
} finally {
cleanup();
}Throw
throw new IllegalArgumentException("Invalid input");Streams API
4 snippetsFilter
list.stream().filter(x -> x > 0).toList();Map
list.stream().map(String::toUpperCase).toList();Reduce
nums.stream().reduce(0, Integer::sum);Collect
stream.collect(Collectors.toList());