0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
Java · BeginnerSnippet

Variables and Primitive Data Types

In Java, every variable must have a declared data type. 'int' is used for whole numbers, 'String' for text, and 'double' for decimal numbers.

snippet.java
java
1
2
3
4
int age = 25;
String name = "Java";
double price = 19.99;
System.out.println(name + " is " + age + " years old.");
SyntaxData Types
Java · BeginnerSnippet

Defining and Calling Methods

Methods are reusable blocks of code. 'void' means the method does not return a value, and parameters allow you to pass data into the method.

snippet.java
java
1
2
3
4
5
6
public static void greet(String user) {
System.out.println("Hello, " + user);
}
// Calling the method
greet("Alice");
FunctionsSyntax
Java · BeginnerSnippet

Conditional Logic with If-Else

The 'if' statement allows you to execute code only if a specific condition is true. The 'else' block provides an alternative if the condition is false.

snippet.java
java
1
2
3
4
5
6
int score = 85;
if (score >= 50) {
System.out.println("Passed!");
} else {
System.out.println("Failed.");
}
Control FlowSyntax
Java · BeginnerSnippet

Basic Class and Object Creation

In Object-Oriented Programming (OOP), a class is a blueprint. An object is an instance of that blueprint created using the 'new' keyword.

snippet.java
java
1
2
3
4
5
6
7
8
9
class Dog {
String breed = "Labrador";
void bark() {
System.out.println("Woof!");
}
}
Dog myDog = new Dog();
myDog.bark();
OOP
Java · BeginnerSnippet

Iterating with For Loops

A 'for' loop is used when you know exactly how many times you want to repeat a block of code. It consists of initialization, condition, and increment.

snippet.java
java
1
2
3
for (int i = 1; i <= 3; i++) {
System.out.println("Iteration: " + i);
}
Control FlowSyntax
Java · BeginnerSnippet

The Switch Statement

The switch statement allows you to select one of many code blocks to be executed based on the value of a variable. It is often cleaner than multiple nested if-else statements.

snippet.java
java
1
2
3
4
5
6
int day = 2;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Other day");
}
Control FlowSyntax
Java · BeginnerSnippet

String Length and Concatenation

Strings in Java are objects representing sequences of characters. You can use the length() method to find the size and the '+' operator to join strings together.

snippet.java
java
1
2
3
String text = "Java";
int length = text.length();
String greeting = "Hello " + text + "!";
Data TypesSyntax
Java · BeginnerSnippet

Basic Exception Handling

Java uses try-catch blocks to handle runtime errors (exceptions). This prevents the program from crashing by providing a way to respond to the error gracefully.

snippet.java
java
1
2
3
4
5
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
}
Error HandlingBest Practices