Java Programming Fundamentals
Java Basics
The Anatomy of a Java Program
Every Java program has a specific structure. Think of it as the basic grammar you need to know. All your code will live inside something called a class, and every program starts running from a special entry point called the main method.
Let's look at the classic "Hello, World!" program. It's the traditional first program for anyone learning a new language.
// All Java code is inside a class. We've named ours 'HelloWorld'.
public class HelloWorld {
// This is the main method, the program's starting point.
public static void main(String[] args) {
// This line prints the text "Hello, World!" to the console.
System.out.println("Hello, World!");
}
}
Breaking it down:
public class HelloWorld { ... }: This defines a class namedHelloWorld. For now, just think of a class as a container for your program's code.public static void main(String[] args) { ... }: This is themainmethod. When you run your Java program, the code inside these curly braces is the first thing that executes.System.out.println("Hello, World!");: This is a statement that tells the system to print a line of text. Notice it ends with a semicolon;. Every statement in Java must end with one. It's like the period at the end of a sentence.
Variables and Data Types
A variable is a container for storing data. Imagine a labeled box where you can keep a piece of information. To create a variable in Java, you must specify what type of data it will hold and give it a name.
Java has several built-in data types, but let's start with the most common ones.
| Data Type | Description | Example |
|---|---|---|
int | Stores whole numbers | 10, -5, 2024 |
double | Stores floating-point (decimal) numbers | 3.14, -0.001, 99.9 |
boolean | Stores true or false values | true |
char | Stores single characters | 'A', '!', '7' |
String | Stores sequences of characters (text) | "Hello Java" |
Note that char values use single quotes (' '), while String values use double quotes (" "). While String is technically a class and not a primitive type like the others, it's so fundamental that we treat it as a basic type for now.
Here's how you declare variables and assign them values:
// Declaring an integer variable named 'age' and assigning it the value 30.
int age = 30;
// A double for a price.
double price = 19.99;
// A boolean to track if the game is over.
boolean isGameOver = false;
// A char for a student's grade.
char grade = 'A';
// A String for a user's name.
String username = "JavaFan123";
You declare a variable once, but you can change its value later as long as the new value is of the same type.
int score = 0; // Initial score is 0
System.out.println(score); // Prints 0
score = 100; // Update the score
System.out.println(score); // Prints 100
Operators
Operators are symbols that perform operations on variables and values. You're already familiar with many of them from basic math.
Java has several kinds of operators.
- Arithmetic Operators: Used for mathematical calculations. These include
+(addition),-(subtraction),*(multiplication), and/(division). There's also the modulo operator,%, which gives you the remainder of a division. - Comparison Operators: Used to compare two values. They are
==(equal to),!=(not equal to),>(greater than),<(less than),>=(greater than or equal to), and<=(less than or equal to). The result of a comparison is always abooleanvalue (trueorfalse). - Logical Operators: Used to combine
booleanexpressions. The main ones are&&(and),||(or), and!(not).
// Arithmetic Operators
int a = 10;
int b = 3;
System.out.println(a + b); // 13
System.out.println(a / b); // 3 (integer division drops the remainder)
System.out.println(a % b); // 1 (the remainder of 10 / 3)
// Comparison Operators
int playerLevel = 5;
boolean isExpert = playerLevel > 10; // false
boolean isTied = (5 == 5); // true
// Logical Operators
boolean hasKey = true;
boolean doorLocked = true;
// Can we open the door? Only if we have the key AND the door is not locked.
boolean canOpen = hasKey && !doorLocked; // false
Making Decisions and Repeating Code
So far, our code runs straight from top to bottom. Control flow statements let us change that. We can make decisions and repeat actions based on certain conditions.
Control flow lets your program make choices and perform repetitive tasks, bringing it to life.
The most common way to make a decision is with an if-else statement. It checks if a condition is true. If it is, one block of code runs. If not, another block (the else part) can run instead.
int temperature = 75;
if (temperature > 80) {
System.out.println("It's hot outside.");
} else if (temperature < 60) {
System.out.println("Bring a jacket.");
} else {
System.out.println("It feels great!");
}
To repeat a block of code, we use loops. A for loop is great when you know exactly how many times you want to repeat something.
// This loop will run 5 times.
// 'i' starts at 0 and increases by 1 each time, until it's no longer < 5.
for (int i = 0; i < 5; i++) {
System.out.println("Current count: " + i);
}
A while loop is useful when you want to repeat code as long as a condition remains true, but you don't know ahead of time how many repetitions that will be.
int fuel = 10;
while (fuel > 0) {
System.out.println("Engine running. Fuel left: " + fuel);
fuel = fuel - 1; // Decrease fuel
}
System.out.println("Out of fuel!");
Now let's check your understanding of these core concepts.
What is the special method that serves as the entry point for every Java application?
Which data type is most appropriate for storing a person's age as a whole number, like 30?
That's a quick tour of the absolute basics. Mastering variables, operators, and control flow is the first big step to becoming a Java programmer. Everything else you learn will build on this foundation.