Java Programming Fundamentals
Java Basics
Setting Up Your Workspace
Before writing any Java code, you need two key pieces of software: the Java Development Kit (JDK) and an Integrated Development Environment (IDE).
The JDK is a software package that contains everything you need to compile, debug, and run Java applications. Think of it as the toolbox that contains the compiler (which turns your human-readable code into machine-readable code) and the Java Virtual Machine (JVM), which actually runs your program.
An IDE is a text editor with superpowers. It helps you write code more efficiently with features like syntax highlighting, code completion, and debugging tools. Popular choices include IntelliJ IDEA, Eclipse, and Visual Studio Code with Java extensions. Once you have a JDK and an IDE installed, you're ready to start coding.
Your First Java Program
The traditional first step in learning a new language is to make it say "Hello, World!". Let's do that in Java. This simple program will print that phrase to your console.
public class HelloWorld {
// The main method is the entry point of any Java application.
public static void main(String[] args) {
// This line prints the text inside the parentheses to the console.
System.out.println("Hello, World!");
}
}
Let's break down this code:
-
public class HelloWorld { ... }: Every Java application has at least one class. A class is a blueprint for creating objects, but for now, just think of it as a container for your program's code. We've named our classHelloWorld. -
public static void main(String[] args) { ... }: This is themainmethod. When you run a Java program, the JVM looks for this exact method to start execution. It's the front door to your program. -
System.out.println("Hello, World!");: This is the statement that does the work.System.out.println()is a built-in Java command that prints a line of text to the console. The semicolon;at the end marks the end of the statement, which is required in Java.
Data and Variables
Programs work by manipulating data. In Java, we store data in variables. A variable is like a labeled box where you can keep a piece of information. Before you can use a variable, you must declare it, which means giving it a type and a name.
Java is a statically-typed language. This means you must specify the type of data a variable will hold right from the start, and it can't be changed later.
Java has several built-in, or primitive, data types. These are the most basic kinds of data.
| Data Type | Description | Example |
|---|---|---|
int | Whole numbers | int age = 30; |
double | Numbers with decimal points | double price = 19.99; |
boolean | true or false values | boolean isLoggedIn = true; |
char | A single character | char grade = 'A'; |
Notice how we declare a variable: type name = value;. We state the type, give it a name, and then use the = assignment operator to give it an initial value.
// Declaring and initializing variables
int score = 100;
double temperature = 98.6;
boolean isRaining = false;
char initial = 'J';
Making Things Happen
Simply storing data isn't very useful. We need to be able to perform operations on it and make decisions. This is where operators and control flow statements come in.
Operator
noun
A symbol that tells the compiler to perform a specific mathematical or logical manipulation.
Java uses standard arithmetic operators you already know: + (addition), - (subtraction), * (multiplication), and / (division). We can use these to create expressions.
int a = 10;
int b = 5;
int sum = a + b; // sum is 15
int product = a * b; // product is 50
int quotient = a / b; // quotient is 2
To control the path our program takes, we use control flow statements. The most common is the if statement, which executes a block of code only if a certain condition is true.
int score = 85;
if (score > 60) {
System.out.println("You passed!");
}
To repeat actions, we use loops. The for loop is great when you know exactly how many times you want to repeat something.
// This loop prints numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
This loop initializes a counter variable i to 1. It continues as long as i <= 5 is true, and after each pass, it increments i by one using i++.
The while loop is used when you want to repeat an action as long as a condition remains true, but you might not know how many repetitions it will take.
int countdown = 3;
while (countdown > 0) {
System.out.println(countdown);
countdown = countdown - 1; // Decrease the counter
}
System.out.println("Blast off!");
These building blocks—variables, operators, and control flow—are the foundation of all Java programs. Mastering them is the first step toward writing powerful and complex applications.