Java Program to Swap Two Variables
Swaps variables using a temporary variable.
4 3
Start with beginner-friendly Java—printing and basic syntax. These concise examples build confidence and show how Java compiles and runs simple programs.
Swaps variables using a temporary variable.
4 3
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
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
Determines if a number is prime.
true
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.