Module 1: The First Steps
Setting up your environment and writing your first lines of Java code.
Welcome to Your Java Journey!
This is where it all begins. Before you can build amazing applications, you need to set up your tools and learn the absolute fundamentals of the Java language. In this module, we will guide you through installing everything you need, writing and running your very first program, and understanding the core concepts of variables and data types. Let's get started!
1Setting Up Your Development Environment
To write and run Java code, you need two key pieces of software: the **Java Development Kit (JDK)** and an **Integrated Development Environment (IDE)**.
What is the JDK?
The JDK is the engine. It includes the **Java Compiler**, which turns your human-readable code into Java bytecode, and the **Java Virtual Machine (JVM)**, which executes that bytecode. It's the essential foundation for any Java development.
- Download: Go to the official Oracle JDK download page. We recommend the latest LTS (Long-Term Support) version.
- Install: Run the installer for your operating system (Windows, macOS, or Linux) and follow the on-screen instructions.
- Verify: Open a new terminal or command prompt and type `java -version`. If the installation was successful, you'll see the version number of your newly installed JDK.
What is an IDE?
The IDE is your workshop. It's a smart code editor that makes writing code much easier with features like syntax highlighting, code completion, and debugging tools. For Java, the two most popular choices are **IntelliJ IDEA** and **Eclipse**.
- Download: We recommend starting with the IntelliJ IDEA Community Edition. It's free and incredibly powerful.
- Install: Run the installer and accept the default settings.
2Your First Program: "Hello, World!"
It's a tradition for programmers to start with a program that prints "Hello, World!" to the screen. Let's create one in IntelliJ IDEA.
- Create a New Project: Open IntelliJ and click "New Project". Give it a name (e.g., "MyFirstApp"), ensure your newly installed JDK is selected, and click "Create".
- Create a New Class: In the project panel on the left, right-click the `src` folder and select `New` -> `Java Class`. Name the class `Main`.
- Write the Code: Inside the `Main.java` file, type the following code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Don't worry about what every word means just yet. The key line is `System.out.println()`, which is Java's command for printing text to the console.
- Run the Program: Click the green "play" button next to the `main` method. You should see "Hello, World!" appear in the console window at the bottom of the screen.
3Variables and Primitive Data Types
A **variable** is a container for storing a piece of data. In Java, every variable must have a specific **data type**, which tells the computer what kind of data it can hold. Java has eight "primitive" (basic) data types. Here are the most common ones:
- `int`: For whole numbers (e.g., 10, -5, 0).
- `double`: For floating-point numbers (e.g., 3.14, -0.5).
- `boolean`: For true or false values.
- `char`: For a single character (e.g., 'A', '$').
To create a variable, you declare its type, give it a name, and assign it a value:
// Declaring and initializing variables
int userAge = 30;
double itemPrice = 19.99;
boolean isLoggedIn = true;
char grade = 'A';
// You can then use these variables
System.out.println("The user's age is: " + userAge);
4Basic Operators
Operators are special symbols that perform operations on variables and values.
Arithmetic Operators
Used for performing mathematical calculations:
- `+` (Addition)
- `-` (Subtraction)
- `*` (Multiplication)
- `/` (Division)
- `%` (Modulus - gives the remainder of a division)
int result = 10 + 5; // result is 15
int remainder = 10 % 3; // remainder is 1
Assignment Operator
The `=` sign is used to assign a value to a variable.
int score = 100; // Assigns 100 to score