Java Programming Fundamentals
Java Basics Setup
Getting Started with Java
Java is a high-level, object-oriented programming language developed by Sun Microsystems in the mid-1990s. Its core philosophy has always been summed up by the motto: Write once, run anywhere (WORA). The idea was to create code that could run on any machine, regardless of its underlying operating system, without needing to be rewritten.
This platform independence is Java's defining feature. A Java program isn't compiled into machine code for a specific processor, like C++ might be. Instead, it's compiled into an intermediate format called bytecode. This bytecode is what runs on any device that has a Java Virtual Machine installed, making Java incredibly portable.
The Java Power Trio
To work with Java, you'll constantly encounter three key acronyms: JDK, JRE, and JVM. They represent a hierarchy of tools, each one containing the next.
Here’s the breakdown:
- JVM (Java Virtual Machine): This is the engine that actually runs the compiled Java bytecode. You rarely interact with it directly, but it’s the component that makes Java portable.
- JRE (Java Runtime Environment): This is what you need to run a Java application. It includes the JVM, core libraries, and other supporting files.
- JDK (Java Development Kit): This is the full toolkit for developing Java applications. It includes everything in the JRE, plus the compiler (
javac), debugger, and other tools needed to turn source code into a runnable program.
As a developer, you will install the JDK. Your users only need the JRE to run your application.
Setting Up Your Environment
First, you need to install the JDK. Oracle provides the official version, but many developers prefer open-source alternatives like OpenJDK. We recommend starting with the latest Long-Term Support (LTS) version of OpenJDK for stability.
After installing the JDK, you'll need a place to write code. While you can use a simple text editor, an Integrated Development Environment makes life much easier. An IDE combines a code editor, build tools, and a debugger into one application. For Java, popular choices are IntelliJ IDEA, Eclipse, and Visual Studio Code with Java extensions.
These tools provide features like code completion, syntax highlighting, and easy ways to compile and run your code, significantly speeding up development.
Your First Java Program
Let's write the traditional "Hello, World!" program. Create a new file named HelloWorld.java. The file name must match the class name exactly, including the capitalization.
public class HelloWorld {
public static void main(String[] args) {
// This is the main entry point of the program
System.out.println("Hello, World!");
}
}
Let’s break down this structure:
public class HelloWorld: This declares a class namedHelloWorld. In Java, all code lives inside classes. Thepublickeyword means it can be accessed by other classes.public static void main(String[] args): This is the main method. It's the front door to your program. When you run the code, the JVM looks for this exact method signature to start execution.System.out.println("Hello, World!");: This line does the actual work. It calls theprintlnmethod to print the text string to the console.
To run this from the command line, navigate to the directory where you saved the file and execute two commands. First, compile the source code into bytecode:
javac HelloWorld.java
This creates a new file, HelloWorld.class, which contains the platform-independent bytecode. Next, run the bytecode using the java command. Note that you don't include the .class extension.
java HelloWorld
If everything is set up correctly, you'll see Hello, World! printed to your console. You've just written, compiled, and executed your first Java application.
What is the primary principle behind Java's "Write once, run anywhere" (WORA) philosophy?
You have downloaded a Java application that you want to run on your computer. Which of the following components is the minimum required to execute the program?
