0 day streak
Code library

Beginner Java exercises

Read these Beginner Java snippets line by line — each one comes with a written breakdown of what the code does and why.

Java · Beginner

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.

Control FlowSyntax
Open snippet →
Java · Beginner

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.

Control FlowSyntax
Open snippet →
Java · Beginner

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.

Control FlowSyntax
Open snippet →
Java · Beginner

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.

Data TypesSyntax
Open snippet →
Java · Beginner

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.

Error HandlingBest Practices
Open snippet →
Java · Beginner

Iterating with While Loops

A while loop repeatedly executes a block of code as long as a given boolean condition remains true. It is useful when the number of iterations is not known in advance.

Control FlowSyntax
Open snippet →
Java · Beginner

Variables and Data Types

In Java, you must declare the type of a variable before using it. Common types include int for integers, String for text, double for decimal numbers, and boolean for true/false values.

SyntaxData Types
Open snippet →
Java · Beginner

Basic Conditional Logic

The 'if-else' statement allows your program to make decisions based on conditions. If the condition in the parentheses is true, the first block executes; otherwise, the else block runs.

Control Flow
Open snippet →
Java · Beginner

The For Loop

A for loop repeats a block of code a specific number of times. It consists of initialization, a condition, and an increment step.

Control FlowSyntax
Open snippet →
Java · Beginner

Defining a Simple Method

Methods are blocks of code that perform a specific task and can be reused. They can take parameters (input) and return a value (output).

Functions
Open snippet →
Java · Beginner

Basic Class and Object

In Object-Oriented Programming (OOP), a class is a blueprint for creating objects. An object has properties (fields like name) and behaviors (methods like meow).

OOP
Open snippet →
Java · Beginner

Working with Arrays

Arrays are used to store multiple values of the same data type in a single variable. You access elements using their index, starting at 0.

Arrays & ListsSyntax
Open snippet →
Java · Beginner

The While Loop

The while loop repeats a block of code as long as a specified boolean condition is true. It is useful when you don't know exactly how many times to loop beforehand.

Control FlowSyntax
Open snippet →
Java · Beginner

Class Inheritance

Inheritance allows one class (child) to acquire the properties and methods of another class (parent). In Java, we use the 'extends' keyword.

OOP
Open snippet →
Java · Beginner

Common String Methods

Strings in Java are objects that come with built-in methods to perform operations like checking length, changing case, or searching for text.

Data TypesSyntax
Open snippet →
Java · Beginner

Interfaces

An interface is a contract that defines methods a class must implement. It allows for polymorphism and decoupling.

OOP
Open snippet →
Java · Beginner

Method Overloading

Method overloading allows multiple methods in the same class to have the same name but different parameters.

FunctionsOOP
Open snippet →
Java · Beginner

Java Enums

Enums are special types that represent a fixed set of constants, making code more readable and type-safe.

Data TypesOOP
Open snippet →
Java · Beginner

The Static Keyword

Static members belong to the class itself rather than individual instances, meaning they are shared by all objects.

OOPMemory Management
Open snippet →
Java · Beginner

The Do-While Loop

The do-while loop is a post-test loop. Unlike a regular while loop, it executes the code block first and then checks the condition, ensuring the code runs at least once.

Control Flow
Open snippet →
Java · Beginner

The Modulus Operator (%)

The modulus operator (%) returns the remainder of a division between two numbers. It is commonly used to check if a number is even or odd.

Syntax
Open snippet →
Java · Beginner

The Ternary Operator

The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values based on the result.

Control FlowSyntax
Open snippet →
Java · Beginner

Reading User Input with Scanner

The Scanner class is used to read input from various sources, such as the system console. It allows your program to interact with the user by receiving text or numbers.

SyntaxBest Practices
Open snippet →
Java · Beginner

Primitive Data Types

In Java, primitive data types are the most basic data types. They store simple values directly in memory rather than as complex objects.

Data TypesSyntax
Open snippet →
Java · Beginner

The If-Else Statement

The if-else statement allows the program to make decisions by executing different blocks of code based on whether a condition is true or false.

Control FlowSyntax
Open snippet →