Learn Java - 10 Code Examples & CST Typing Practice Test
Java is a robust, object-oriented, platform-independent programming language designed for reliability, performance, and scalability. It powers enterprise systems, Android apps, backend services, banking infrastructure, and large distributed systems used globally.
Learn JAVA with Real Code Examples
Updated Nov 17, 2025
Code Sample Descriptions
Java Collection and Stream API
import java.util.*;
import java.util.stream.Collectors;
public class StreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList(
"Alice", "Bob", "Charlie", "David", "Eve"
);
List<String> filteredNames = names.stream()
.filter(name -> name.length() > 3)
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
System.out.println("Filtered names: " + filteredNames);
Optional<String> longest = names.stream()
.max(Comparator.comparing(String::length));
longest.ifPresent(name ->
System.out.println("Longest name: " + name)
);
}
}
Shows modern Java features including streams, lambda expressions, and collection operations.
Java Basic Class and Object
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
void introduce() {
System.out.println("Hi, I'm " + name + ", age " + age);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person("Alice", 25);
p.introduce();
}
}
Defines a class with properties and methods and demonstrates object creation.
Java Map and HashMap Example
import java.util.*;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> ages = new HashMap<>();
ages.put("Alice", 25);
ages.put("Bob", 30);
for(Map.Entry<String, Integer> entry : ages.entrySet()) {
System.out.println(entry.getKey() + " is " + entry.getValue() + " years old");
}
}
}
Demonstrates using HashMap, adding entries, and iterating over keys and values.
Java List Sorting Example
import java.util.*;
public class SortExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(5, 3, 8, 1, 9);
Collections.sort(numbers);
System.out.println("Sorted: " + numbers);
numbers.sort((a, b) -> b - a);
System.out.println("Reverse sorted: " + numbers);
}
}
Shows sorting a list of integers using Collections.sort and lambda expressions.
Java Exception Handling Example
public class ExceptionExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Execution finished");
}
}
}
Demonstrates try-catch-finally blocks to handle exceptions safely.
Java ForEach with Lambda
import java.util.*;
public class LambdaExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");
fruits.forEach(fruit -> System.out.println(fruit));
}
}
Iterates over a list using forEach and a lambda expression.
Java Optional Example
import java.util.*;
public class OptionalExample {
public static void main(String[] args) {
Optional<String> name = Optional.ofNullable(null);
System.out.println(name.orElse("Default Name"));
}
}
Shows how to use Optional to handle potential null values.
Java Filter and Collect Example
import java.util.*;
import java.util.stream.Collectors;
public class FilterExample {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(1,2,3,4,5,6);
List<Integer> even = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println("Even numbers: " + even);
}
}
Filters a list of integers and collects results using Stream API.
Java Set Example
import java.util.*;
public class SetExample {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("Alice");
set.add("Bob");
set.add("Alice"); // Duplicate ignored
System.out.println(set);
}
}
Demonstrates usage of HashSet to store unique elements.
Java Custom Comparator Example
import java.util.*;
class Person {
String name;
int age;
Person(String name, int age) { this.name = name; this.age = age; }
public String toString() { return name + "(" + age + ")"; }
}
public class ComparatorExample {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 20)
);
people.sort(Comparator.comparingInt(p -> p.age));
System.out.println(people);
}
}
Sorts a list of custom objects using a custom comparator.
Frequently Asked Questions about Java
What is Java?
Java is a robust, object-oriented, platform-independent programming language designed for reliability, performance, and scalability. It powers enterprise systems, Android apps, backend services, banking infrastructure, and large distributed systems used globally.
What are the primary use cases for Java?
Enterprise backend systems. Android application development. Financial/banking systems. Cloud microservices (Spring Boot, Quarkus). Large distributed systems. Big data pipelines (Hadoop, Spark)
What are the strengths of Java?
Extremely reliable and stable. Massive enterprise adoption. High performance with JVM optimizations. Great tooling (IntelliJ, Maven, Gradle). Backwards compatibility across versions
What are the limitations of Java?
Verbose syntax compared to modern languages. Higher memory consumption. Startup time slower than Go/Rust. Requires JVM runtime environment
How can I practice Java typing speed?
CodeSpeedTest offers 10+ real Java code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.