Java Program to Check Prime Number
Determines if a number is prime.
true
Start with beginner-friendly Java—printing and basic syntax. These concise examples build confidence and show how Java compiles and runs simple programs.
Determines if a number is prime.
true
Prints a greeting to the console using System.out.println.
Hello, World!
Adds two integers and prints their sum.
42
Computes square root using Math.sqrt.
9.0
Uses the formula 1/2 * base * height.
25.0
Swaps variables using a temporary variable.
4 3
Computes roots using the quadratic formula.
2.0 1.0
Converts km to miles (1 km ≈ 0.621371 mi).
6.21371
Uses the formula (C*9/5)+32.
77.0
Checks the sign of a number using conditionals.
zero
Uses modulo % 2 to determine parity.
odd
Computes factorial using iterative loop.
120
Prints first N Fibonacci numbers.
0 1 1 2 3 5 8
Counts vowels using regex.
3
Reverses a string using StringBuilder.
avaJ
javac Main.java and run with java Main. On macOS, Linux, or Windows, use the official OpenJDK builds or your package manager.
Java powers enterprise applications, Android development, and countless backend systems. Practicing small, focused programs builds fluency with syntax and core concepts such as data types, control flow, methods, classes, interfaces, exceptions, collections, and streams.
On this page, you will find clean, well-formatted examples that run across platforms. Each example includes a short description and expected output so you can quickly verify your understanding. Explore the topics via the sidebar and extend the code to experiment further.
Whether you are preparing for interviews, working through coursework, or refreshing fundamentals, these examples offer a practical path to mastering Java. Bookmark the page and return often to keep improving.
This collection of Java examples covers fundamental to intermediate concepts with clean, runnable code. Each program is self-contained — copy it into any Java IDE, online compiler, or your local development environment and run it immediately. The examples progress from basic syntax through object-oriented programming, data structures, and algorithms.
Java remains one of the most popular programming languages worldwide, ranking consistently in the top 3 on TIOBE, Stack Overflow, and GitHub. It powers Android apps, enterprise backends (Spring Boot), big data (Hadoop), and financial systems. Practising with small, focused examples is the fastest way to build Java fluency.
Research on skill acquisition shows that deliberate practice — focused, repetitive work on specific concepts — produces faster learning than reading alone. Small Java programs isolate one concept at a time, making it easy to understand, modify, and remember. This is why coding interviews test with small problems: they reveal fundamental fluency that large project experience alone cannot demonstrate.
Each example on this page is designed to be completed in 2–5 minutes, making them perfect for daily practice sessions, interview preparation, or quick reference when you forget a specific syntax pattern.
Most examples use Java 8+ features. Some advanced examples use Java 17 features like records and text blocks. All run on the current LTS (Java 17 or 21).
Yes. The examples cover the most common interview topics: arrays, strings, recursion, OOP, collections, and basic algorithms. Practice modifying them to solve variations.
In Java, the class name must match the filename. Using Main makes examples portable across online compilers that expect this convention. In real projects, you would name classes descriptively.
Yes, completely free. Copy, modify, and use them in your projects, homework, or learning materials without attribution.
CS students practising for exams and assignments. Job seekers preparing for Java technical interviews. Self-taught developers building foundational Java skills. Teachers looking for clean, copyable code examples for lessons. Android developers refreshing core Java knowledge. Backend engineers transitioning from other languages to Java/Spring Boot.