Java Programs for Beginners: A Step-by-Step Practice Set

Beginner Java code running in a console

These Java programs for beginners build from output and variables to input, conditions, loops, methods, arrays, and objects. Each example is a complete console program that can compile independently when saved using its public class name as the filename.

Run a compiled class with java ClassName. Type the sample input when the program waits at the console.

Java Programs for Beginners: Output, Variables, and Input

Start with variables and output, then add console input with Scanner.

FirstProgram.java: public class FirstProgram { public static void main(String[] args) { String language = "Java"; int lessons = 7; System.out.println(language + " lessons: " + lessons); } }

Sample run: Input: none. Output: Java lessons: 7

ReadName.java: import java.util.Scanner; public class ReadName { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Name: "); String name = input.nextLine(); System.out.println("Hello, " + name + "!"); input.close(); } }

Sample run: Input: Mina. Output: Name: Mina, then Hello, Mina!

Simple Java Programs: Conditions and Loops

Conditions choose between outcomes. The remainder operator, %, checks whether a number divides evenly by two.

EvenNumber.java: import java.util.Scanner; public class EvenNumber { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Number: "); int number = input.nextInt(); if (number % 2 == 0) { System.out.println("Even"); } else { System.out.println("Odd"); } input.close(); } }

Sample run: Input: 8. Output: Number: 8, then Even

A for loop repeats a known number of times. Its counter starts at 1, continues through 5, and increases after each iteration.

CountNumbers.java: public class CountNumbers { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println(i); } } }

Sample run: Input: none. Output: 1 2 3 4 5, each number on its own line.

Methods and Arrays in a Basic Java Program

Methods package reusable behavior. Arrays store several values of the same type, and a loop can process every element.

AddNumbers.java: public class AddNumbers { static int add(int first, int second) { return first + second; } public static void main(String[] args) { int total = add(4, 6); System.out.println("Total: " + total); } }

Sample run: Input: none. Output: Total: 10

ArrayTotal.java: public class ArrayTotal { public static void main(String[] args) { int[] scores = {4, 7, 9}; int total = 0; for (int score : scores) { total += score; } System.out.println("Total: " + total); } }

Sample run: Input: none. Output: Total: 20

A Small Class-Based Program That Creates an Object

A class combines data and behavior. The constructor gives each object a title, and show displays it.

BookDemo.java: class Book { String title; Book(String title) { this.title = title; } void show() { System.out.println("Book: " + title); } } public class BookDemo { public static void main(String[] args) { Book book = new Book("Java Basics"); book.show(); } }

Sample run: Input: none. Output: Book: Java Basics