Java Programming Fundamentals
Introduction to Java
What Is Java?
Java is a programming language first released in the mid-1990s by Sun Microsystems. It was designed with a simple goal in mind: "write once, run anywhere." This means a program written on a Mac could run on a Windows PC or a Linux server without any changes. This was a revolutionary idea at the time, and it’s one of the reasons Java became so popular.
The secret to Java's portability is the Java Virtual Machine, or JVM. Think of the JVM as a universal translator. Instead of writing code for a specific operating system, you write code for the JVM. The JVM then translates your program into instructions that the local machine can understand.
Java is also an object-oriented programming (OOP) language. This is a way of organizing code that models the real world. In OOP, you create "objects" that have their own data and behaviors. For example, you could create a Dog object with data like breed and age, and behaviors like bark() or wagTail(). This approach helps keep code organized, reusable, and easier to manage, especially in large, complex applications.
Setting Up Your Tools
Before you can write Java code, you need the right tools. The most important one is the Java Development Kit, or JDK. The JDK includes everything you need to create and run Java applications.
The JDK comes with two critical components: a compiler (
javac) and a runtime environment (java). The compiler takes your human-readable code and turns it into Java bytecode, which is what the JVM understands. The runtime environment is what actually executes that bytecode.
You can download the JDK for free from Oracle or from other providers like Adoptium. Installation is straightforward, and once it's set up, you can use your computer's command line or terminal to compile and run your programs.
Your First Program
It's a tradition for programmers to start with a program that prints "Hello, World!" to the screen. Let's write one in Java. Create a file named HelloWorld.java and type the following code into it.
public class HelloWorld {
public static void main(String[] args) {
// This line prints the text to the screen
System.out.println("Hello, World!");
}
}
Let's break that down.
public class HelloWorld: Every Java application needs at least one class. A class is a container for your code. The name of the class,HelloWorld, must match the filename,HelloWorld.java.public static void main(String[] args): This is the main method. It’s the entry point of your program. When you run the application, the JVM looks for this exact method and starts executing the code inside it.System.out.println("Hello, World!");: This is the instruction that does the work. It tells the system to print the text inside the parentheses to the console.
To run this program, open your terminal, navigate to the directory where you saved your file, and run two commands.
First, compile the code:
javac HelloWorld.java
This command invokes the Java compiler, which creates a new file called HelloWorld.class. This file contains the Java bytecode. If there are any errors in your code, the compiler will let you know here.
Now, run the program:
java HelloWorld
This command starts the JVM and tells it to execute the code in the HelloWorld class. Notice you don't include the .class extension. If everything works, you'll see Hello, World! printed in your terminal.
Ready to check your understanding?
What does the Java design philosophy "write once, run anywhere" mean?
In a basic Java application, what is the role of the public static void main(String[] args) method?
That's it! You've successfully set up your environment, written your first Java program, and learned how to compile and run it. You're now ready to explore more of what Java has to offer.