Java Programs Examples With Output W3schools Tutorial Guide

Robert Jul 13, 2026

Java, a robust and widely-used programming language, offers a range of applications and is known for its versatility. When learning Java, it's helpful to explore practical examples to understand its syntax and functionality better. W3Schools provides an array of Java programs with their respective outputs, which can be a goldmine for beginners and seasoned developers alike.

Java Cheatsheet
Java Cheatsheet

Let's delve into some Java program examples from W3Schools, along with their outputs, to enhance your understanding and skill set.

Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻

Simple Java Programs

These programs demonstrate basic Java syntax and concepts.

30 Days of Java – Day 9 | Java Methods Explained for Beginners
30 Days of Java – Day 9 | Java Methods Explained for Beginners

First, let's look at a simple "Hello, World!" program.

Hello, World!

Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners

The following code prints "Hello, World!" on the console.

```java public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } ```

The output of this program is:

```bash Hello, World! ```

Data Types & Variables

Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide

This program showcases different data types and variables in Java.

```java public class Main { public static void main(String[] args) { int myAge = 30; double mySalary = 40000.50; String myName = "John"; System.out.println(myName + " is " + myAge + " years old."); System.out.println("He earns " + mySalary + " per year."); } } ```

The output of this program is:

```bash John is 30 years old. He earns 40000.5 per year. ```

Control Structures

Basic GUI in Java | Swing GUI Tutorial for Beginners
Basic GUI in Java | Swing GUI Tutorial for Beginners

Java's control structures allow us to control the flow of our programs.

Let's explore an example of an if-else statement.

Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Java Projects 2023
Java Projects 2023
Java Project Ideas For Beginners
Java Project Ideas For Beginners
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
Java Program to Print Multiplication Table
Java Program to Print Multiplication Table
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
9 great java project ideas for beginners to use in the web design process
9 great java project ideas for beginners to use in the web design process
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
concept of java #java  #javanotes
concept of java #java #javanotes
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Java Roadmap 2026 ☕ | Complete Beginner to Advanced Learning Guide
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
Java While Loop Explained 🔁 | Easy Guide for Beginners
Java While Loop Explained 🔁 | Easy Guide for Beginners
Title: 🎯 Switch Case in Java Simplified!
Title: 🎯 Switch Case in Java Simplified!
Java Programming Cheat Sheet
Java Programming Cheat Sheet
Java OOP concepts cheat sheet
Java OOP concepts cheat sheet
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀

IF-ELSE Statement

This program checks if a number is positive, negative, or zero.

```java public class Main { public static void main(String[] args) { int num = -5; if (num > 0) { System.out.println(num + " is positive."); } else if (num < 0) { System.out.println(num + " is negative."); } else { System.out.println(num + " is zero."); } } } ```

The output of this program is:

```bash -5 is negative. ```

For Loop

The following program prints the numbers 1 to 5 using a for loop.

```java public class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println(i); } } } ```

The output of this program is:

```bash 1 2 3 4 5 ```

To fully master Java, it's crucial to practice more examples like these. W3Schools offers an extensive collection of Java programs, along with their outputs, to help you learn and understand concepts better.

So, start exploring, writing, and running your own Java programs today. The more you practice, the more proficient you'll become, and before you know it, you'll be developing impressive Java applications.