Java Platform Fundamentals and Hello World Application
Getting Started with Java
Java technology consists of both a programming language and a runtime platform. The language is high-level, object-oriented, and designed for simplicity and portability. The platform provides the necessary environment to execute Java applications through the Java Virtual Machine (JVM) and associated libraries.
The Java compiler (javac) translates source code files (.java) into bytecode (.class), which is platform-independent. This bytecode runs on any system with a compatible JVM, enabling "write once, run anywhere" capability.
The Java platform includes:
- Java Virtual Machine: Executes compiled bytecode
- Java Application Programming Interface (API): Extensive library of pre-built componants organized in packages
Creating Your First Java Application
Prerequisites
Before writing your program, ensure you have:
- Java Development Kit (JDK)
- Text editor or Integrated Development Environment (IDE)
Writing Source Code
Create a file named HelloWorldApp.java with the following content:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Key points when typing:
- Java is case-sensitive (
HelloWorldApp≠helloworldapp) - Class name must match filename exactly
- Every application requires a
mainmethod as entry point
Compilation Process
Compile the source file using:
javac HelloWorldApp.java
This generates HelloWorldApp.class containing bytecode instructions for the JVM.
Execution
Run the compiled program with:
java HelloWorldApp
Expected output:
Hello World!
Understanding the Hello World Program Structure
The example contains three fundamental elements:
Comments
Java supports three comment styles ignored by the compiler:
/* text */: Multi-line comments// text: Single-line comments/** documentation */: Documentation comments processed by javadoc tool
Class Definition
Every Java application begins with a class definition:
class ClassName {
// class body
}
The class acts as a blueprint containing all program functionality.
Main Method
Applications must include a main method with signature:
public static void main(String[] args)
This serves as the execution starting point where the JVM begins program flow.
Java Language Basics
Variables
Java defines several variable types:
- Instance variables: Non-static fields unique to each object instance
- Class variables: Static fields shared across all instances
- Local variables: Temporary values within method scope
- Parameters: Variables receiving argumant values
Variable naming follows these conventions:
- Begin with letter, underscore (_), or dollar sign ($)
- Subsequent characters can include digits
- Case-sensitive identifiers
- Use camelCase for multi-word names
Primitive Data Types
Eight built-in data types provide basic value storage:
| Type | Size | Range |
|---|---|---|
| byte | 8-bit | -128 to 127 |
| short | 16-bit | -32,768 to 32,767 |
| int | 32-bit | -2³¹ to 2³¹-1 |
| long | 64-bit | -2⁶³ to 2⁶³-1 |
| float | 32-bit | IEEE 754 floating-point |
| double | 64-bit | IEEE 754 double-precision |
| boolean | 1-bit | true or false |
| char | 16-bit | '\u0000' to '\uffff' (Unicode characters) |
Fields receive default values when uninitialized:
- Numeric types: 0
- boolean: false
- char: '\u0000'
- Object references: null
Arrays
Arays store fixed-size sequences of elements with same type:
Declaration:
int[] numbers;
Initialization:
numbers = new int[10]; // Creates array with 10 elements
Alternative syntax:
int[] values = {1, 2, 3, 4, 5};
Access elements via zero-based indexing:
System.out.println(values[0]); // Prints first element
Multi-dimensional arrays:
String[][] matrix = {
{"A", "B"},
{"C", "D"}
};
System.out.println(matrix[0][1]); // Prints "B"
Array utility operations available through java.util.Arrays:
- Copying:
copyOfRange() - Sorting:
sort(),parallelSort() - Searching:
binarySearch() - Comparison:
equals() - Conversion:
toString()