Java Basics Examples

Start with beginner-friendly Java—printing and basic syntax. These concise examples build confidence and show how Java compiles and runs simple programs.

Java Program to Check Prime Number

Program (Java)
Example & Expected Output

Determines if a number is prime.

true
Program Output

                                    

Java "Hello, World!" Program

Program (Java)
Example & Expected Output

Prints a greeting to the console using System.out.println.

Hello, World!
Program Output

                                    

Java Program to Add Two Numbers

Program (Java)
Example & Expected Output

Adds two integers and prints their sum.

42
Program Output

                                    

Java Program to Find the Square Root

Program (Java)
Example & Expected Output

Computes square root using Math.sqrt.

9.0
Program Output

                                    

Java Program to Calculate the Area of a Triangle

Program (Java)
Example & Expected Output

Uses the formula 1/2 * base * height.

25.0
Program Output

                                    

Java Program to Swap Two Variables

Program (Java)
Example & Expected Output

Swaps variables using a temporary variable.

4 3
Program Output

                                    

Java Program to Solve a Quadratic Equation

Program (Java)
Example & Expected Output

Computes roots using the quadratic formula.

2.0 1.0
Program Output

                                    

Java Program to Convert Kilometers to Miles

Program (Java)
Example & Expected Output

Converts km to miles (1 km ≈ 0.621371 mi).

6.21371
Program Output

                                    

Java Program to Convert Celsius to Fahrenheit

Program (Java)
Example & Expected Output

Uses the formula (C*9/5)+32.

77.0
Program Output

                                    

Java Program to Check if a Number is Positive, Negative, or Zero

Program (Java)
Example & Expected Output

Checks the sign of a number using conditionals.

zero
Program Output

                                    

Java Program to Check if a Number is Odd or Even

Program (Java)
Example & Expected Output

Uses modulo % 2 to determine parity.

odd
Program Output

                                    

Java Program to Compute Factorial

Program (Java)
Example & Expected Output

Computes factorial using iterative loop.

120
Program Output

                                    

Java Program to Print Fibonacci Series

Program (Java)
Example & Expected Output

Prints first N Fibonacci numbers.

0 1 1 2 3 5 8
Program Output

                                    

Java Program to Count Vowels in a String

Program (Java)
Example & Expected Output

Counts vowels using regex.

3
Program Output

                                    

Java Program to Reverse a String

Program (Java)
Example & Expected Output

Reverses a string using StringBuilder.

avaJ
Program Output

                                    

Frequently Asked Questions

Install a JDK (Java Development Kit), then compile with javac Main.java and run with java Main. On macOS, Linux, or Windows, use the official OpenJDK builds or your package manager.

The examples target modern Java (Java 8 and above). If you use older versions, most examples will still work, but features like lambdas and streams require Java 8+.

Yes. These examples are intended for learning. Copy the code, experiment with changes (e.g., different inputs), and observe the output to deepen your understanding of Java fundamentals.

Yes. Java is platform-independent. The examples compile and run on major platforms (Windows, macOS, Linux) with a standard JDK.

Learn Java the Practical Way

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.