Back to notes index
These examples are collected from the chapters. Check each source chapter for Java version requirements before compiling its examples.
java --version
javac --version
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
javac Main.java
java Main
jshell> int count = 3;
jshell> count * 2
$2 ==> 6
int age = 28;
double temperature = 21.5;
char initial = 'A';
boolean isReady = true;
String language = "Java";
int itemCount = 12;
long population = 8_000_000_000L;
double price = 19.95;
float scale = 0.75f;
char grade = 'B';
boolean available = false;
String firstName = "Ashish";
String greeting = "Hello, " + firstName;
Integer score = 95;
var message = "Study Java";
var attemptCount = 3;
int count = 5;
double exactCount = count;
double measurement = 7.8;
int wholePart = (int) measurement;
int total = 17;
int people = 5;
int wholePerPerson = total / people;
int remainder = total % people;
double average = (double) total / people;
int score = 78;
boolean passed = score >= 60;
boolean eligible = passed && score < 100;
boolean needsReview = score < 60 || score > 100;
int score = 78;
if (score >= 60) {
System.out.println("Passed");
} else {
System.out.println("Try again");
}
String level = switch (score / 10) {
case 10, 9 -> "excellent";
case 8 -> "strong";
case 7, 6 -> "passing";
default -> "keep practicing";
};
for (int index = 0; index < 3; index++) {
System.out.println("Attempt " + index);
}
int remaining = 3;
while (remaining > 0) {
System.out.println("Remaining: " + remaining);
remaining--;
}
public class MethodExamples {
static int add(int first, int second) {
return first + second;
}
static void printGreeting(String name) {
System.out.println("Hello, " + name);
}
public static void main(String[] args) {
int total = add(4, 7);
printGreeting("Maya");
System.out.println(total);
}
}
static String displayName(String name) {
if (name == null || name.isBlank()) {
return "Guest";
}
return name.trim();
}
static int multiply(int first, int second) {
return first * second;
}
static double multiply(double first, double second) {
return first * second;
}
static void appendMark(StringBuilder text) {
text.append("!");
}
static void replaceLocalReference(StringBuilder text) {
text = new StringBuilder("New value");
}
StringBuilder message = new StringBuilder("Hello");
appendMark(message);
replaceLocalReference(message);
System.out.println(message); // Hello!
static int sum(int... values) {
int result = 0;
for (int value : values) {
result += value;
}
return result;
}
int[] scores = {82, 95, 74};
scores[0] = 88;
System.out.println(scores[0]);
System.out.println(scores.length);
int[] scores = {88, 95, 74};
for (int index = 0; index < scores.length; index++) {
System.out.println(index + ": " + scores[index]);
}
for (int score : scores) {
System.out.println(score);
}
import java.util.Arrays;
int[] scores = {88, 95, 74};
System.out.println(Arrays.toString(scores));
String course = " Java ";
String cleaned = course.trim();
System.out.println(cleaned);
System.out.println(course);
String selectedCourse = new String("Java");
if ("Java".equals(selectedCourse)) {
System.out.println("Course selected");
}
StringBuilder report = new StringBuilder();
report.append("Completed ");
report.append(3);
report.append(" chapters");
String message = report.toString();
System.out.println(message);
class Book {
private final String title;
private final String author;
Book(String title, String author) {
this.title = title;
this.author = author;
}
String getTitle() {
return title;
}
String summary() {
return title + " by " + author;
}
}
public class Main {
public static void main(String[] args) {
Book first = new Book("Clean Code", "Robert C. Martin");
Book second = new Book("Effective Java", "Joshua Bloch");
System.out.println(first.summary());
System.out.println(second.getTitle());
}
}
class BankAccount {
private int balanceCents;
public int getBalanceCents() {
return balanceCents;
}
public void deposit(int amountCents) {
if (amountCents <= 0) {
throw new IllegalArgumentException("Deposit must be positive");
}
balanceCents += amountCents;
}
}
class Employee {
private final String name;
Employee(String name) {
this.name = name;
}
String getName() {
return name;
}
}
class Manager extends Employee {
private final int teamSize;
Manager(String name, int teamSize) {
super(name);
this.teamSize = teamSize;
}
int getTeamSize() {
return teamSize;
}
}
class Engine {
void start() {
System.out.println("Engine started");
}
}
class Car {
private final Engine engine;
Car(Engine engine) {
this.engine = engine;
}
void start() {
engine.start();
}
}
interface Printable {
String format();
}
class Note implements Printable {
private final String text;
Note(String text) {
this.text = text;
}
@Override
public String format() {
return "Note: " + text;
}
}
enum Priority {
LOW,
NORMAL,
HIGH
}
Priority taskPriority = Priority.HIGH;
record Point(double x, double y) {}
Point origin = new Point(0, 0);
System.out.println(origin.x());
System.out.println(origin.y());
record Temperature(double celsius) {
Temperature {
if (celsius < -273.15) {
throw new IllegalArgumentException("Temperature is below absolute zero");
}
}
}
String input = "42";
try {
int quantity = Integer.parseInt(input);
System.out.println("Quantity: " + quantity);
} catch (NumberFormatException exception) {
System.out.println("Enter a whole number.");
}
static int requirePositive(int value) {
if (value <= 0) {
throw new IllegalArgumentException("Value must be positive");
}
return value;
}
class Box<T> {
private final T value;
Box(T value) {
this.value = value;
}
T getValue() {
return value;
}
}
Box<String> title = new Box<>("Java notes");
String text = title.getValue();
static <T> T first(T left, T right) {
return left;
}
String selected = first("Java", "HTML");
Integer count = first(3, 5);
static <T extends Number> double asDouble(T value) {
return value.doubleValue();
}
double result = asDouble(12);
static double sum(java.util.List<? extends Number> values) {
double total = 0;
for (Number value : values) {
total += value.doubleValue();
}
return total;
}
import java.util.ArrayList;
import java.util.List;
List<String> names = new ArrayList<>();
names.add("Maya");
names.add("Ravi");
names.add("Maya");
System.out.println(names.get(0));
System.out.println(names.size());
import java.util.HashSet;
import java.util.Set;
Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("collections");
tags.add("java");
System.out.println(tags.size());
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> scores = new HashMap<>();
scores.put("Maya", 92);
scores.put("Ravi", 85);
Integer mayaScore = scores.get("Maya");
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
List<String> fixedNames = List.of("Maya", "Ravi");
List<String> editableNames = new ArrayList<>(fixedNames);
editableNames.add("Tara");
@FunctionalInterface
interface TextCheck {
boolean accepts(String text);
}
TextCheck hasContent = text -> !text.isBlank();
System.out.println(hasContent.accepts("Java"));
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
Predicate<String> isLong = text -> text.length() > 8;
Function<String, Integer> length = text -> text.length();
Consumer<String> print = text -> System.out.println(text);
Supplier<String> message = () -> "Ready";
System.out.println(isLong.test("Study Java"));
System.out.println(length.apply("Java"));
print.accept(message.get());
import java.util.List;
List<String> names = List.of("Maya", "Ravi", "Tara");
names.forEach(System.out::println);
import java.util.ArrayList;
import java.util.List;
List<String> names = new ArrayList<>(List.of("Maya", "", "Ravi"));
names.removeIf(name -> name.isBlank());
System.out.println(names);
import java.util.List;
List<String> names = List.of("Maya", "Ravi", "Marina", "Tara");
List<String> result = names.stream()
.filter(name -> name.length() >= 5)
.map(String::toUpperCase)
.sorted()
.toList();
System.out.println(result);
import java.util.List;
int totalLength = List.of("HTML", "Java", "CSS")
.stream()
.mapToInt(String::length)
.sum();
System.out.println(totalLength);
import java.util.List;
import java.util.Optional;
List<String> names = List.of("Maya", "Ravi", "Tara");
Optional<String> match = names.stream()
.filter(name -> name.startsWith("R"))
.findFirst();
String displayName = match
.map(String::toUpperCase)
.orElse("No match");
System.out.println(displayName);
import java.nio.file.Path;
Path notesFile = Path.of("notes", "today.txt");
System.out.println(notesFile);
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Path file = Path.of("today.txt");
try {
Files.writeString(file, "Review collections today.");
String content = Files.readString(file);
System.out.println(content);
} catch (IOException exception) {
System.out.println("Could not read or write the file.");
}
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
try (BufferedReader reader = Files.newBufferedReader(Path.of("today.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException exception) {
System.out.println("Could not read the file.");
}
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
LocalDate dueDate = LocalDate.of(2026, 10, 20);
ZonedDateTime meeting = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"));
DateTimeFormatter format = DateTimeFormatter.ofPattern("dd MMM uuuu");
System.out.println(dueDate.format(format));
System.out.println(meeting);
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) throws Exception {
try (ExecutorService executor = Executors.newFixedThreadPool(2)) {
Future<String> result = executor.submit(() -> "Task finished");
System.out.println(result.get());
}
}
}
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger completedTasks = new AtomicInteger();
Runnable task = () -> completedTasks.incrementAndGet();
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class VirtualThreadExample {
public static void main(String[] args) throws Exception {
try (ExecutorService executor =
Executors.newVirtualThreadPerTaskExecutor()) {
Future<String> response = executor.submit(() -> "Response ready");
System.out.println(response.get());
}
}
}
package com.example.notes;
public class Main {
public static void main(String[] args) {
System.out.println("Package ready");
}
}
javac -d out src/com/example/notes/Main.java
java -cp out com.example.notes.Main
module com.example.notes {
exports com.example.notes.api;
}
static int add(int first, int second) {
return first + second;
}
static void testAdd() {
if (add(2, 3) != 5) {
throw new AssertionError("Expected 5");
}
}
jar --create --file app.jar --main-class com.example.notes.Main -C out .
java -jar app.jar