- Java Tutorial
- Java Introduction
- Java Features
- Java Simple Program
- JVM, JDK and JRE
- Java Syntax
- Java Comments
- Java Keywords
- Java Variables
- Java Literals
- Java Separators
- Java Datatypes
- Java Operators
- Java Statements
- Java Strings
- Java Arrays
- Control Statement
- Java If
- Java If-else
- Java If-else-if
- Java Nested If
- Java Switch
- Iteration Statement
- Java For Loop
- Java For Each Loop
- Java While Loop
- Java Do While Loop
- Java Nested Loop
- Java Break/Continue
- Java Methods
- Java Methods
- Java Method Parameters
- Java Method Overloading
- Java Recursion
- Java OOPS
- Java OOPs
- Java Classes/Objects
- Java Inheritance
- Java Polymorphism
- Java Encapsulation
- Java Abstraction
- Java Modifiers
- Java Constructors
- Java Interface
- Java static keyword
- Java this keyword
- Java File Handling
- Java File
- Java Create File
- Java Read/Write File
- Java Delete File
- Java Program To
- Add Two Numbers
- Even or Odd Numbers
- Reverse a String
- Swap Two Numbers
- Prime Number
- Fibonacci Sequence
- Palindrome Strings
- Java Reference
- Java String Methods
- Java Math Methods
Java Simple Program
Every journey in software development begins with a single line of code. In Java, that journey typically starts with a "Hello World" style program. While it may look like just a few lines of text, this simple structure forms the foundation of every enterprise-grade application, Android app, and high-frequency trading platform built with Java today.
Requirements for Writing a Simple Java Program
Before you can write and execute code, you need to set up your development environment. Here is a breakdown of what you need to get started:
- JDK (Java Development Kit): This is the most critical component. It includes the Java Runtime Environment (JRE), the interpreter/loader (java), and the compiler (javac). Without the JDK, you can run Java programs, but you cannot create or compile them.
- Text Editor or IDE:
- For absolute beginners, a simple text editor like Notepad++ or Sublime Text helps you understand the underlying mechanics of compilation.
- For professional development, an Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code provides powerful tools like auto-completion and debugging.
- Java Source Code: This is the file you write, ending in a
.javaextension. It contains the instructions that tell the computer what to do. - Compilation: Computers don't understand Java code directly. You must use the javac command to translate your human-readable code into "Bytecode" (a
.classfile) that the Java Virtual Machine can understand. - Execution: Finally, you run the program using the java command. This triggers the JVM to read your bytecode and perform the actions you've programmed.
Example Simple Java Program
Below is a standard template for a basic Java program. Copy this into a file named SimpleProgram.java.
public class SimpleProgram {
public static void main(String[] args) {
// This line outputs text to the console
System.out.println("Hello, Java!");
}
}
SimpleProgram, your file must be saved as SimpleProgram.java.
Explanation
Let’s pull this code apart to see what each piece does. Understanding these keywords is essential, as they appear in almost every Java file you will ever write.
- Class Declaration:
public class SimpleProgramdefines a blueprint. In Java, everything must exist inside a class. Thepublickeyword means this class is accessible from other classes. - Main Method: This is the "Entry Point" of your application. When you tell Java to run your program, it looks for this specific line:
public static void main(String[] args).- static: Allows the method to run without creating an "instance" (object) of the class first.
- void: This means the method does not return any data once it finishes.
- String[] args: This allows the program to accept inputs (arguments) from the command line when it starts.
- Print Statement:
System.out.printlnis the standard way to send data to the console output. Think ofSystemas the computer system,outas the output destination, andprintlnas the command to "print a line."
system.out.println (lowercase 's') or Main (uppercase 'M') will cause a compilation error.
Test1, use something descriptive like UserRegistration or InvoiceCalculator to make your code easier for other developers to read.
Summary
This simple Java program acts as a gateway to learning the language. By defining a class, creating a main method, and using a print statement, you've successfully navigated the core syntax of Java. From here, you can begin adding logic, variables, and complex data structures to build real-world applications like web servers, data processing tools, or mobile apps.
System.out.println line to see how the output changes. Small experiments are the best way to learn!