Java Fundamentals for Beginners
Java Basics
Your First Java Program
Every journey starts with a single step. In programming, that first step is often a simple program that prints "Hello, World!" to the screen. It's a tradition, and it's the perfect way to see the basic structure of a Java program.
public class HelloWorld {
public static void main(String[] args) {
// This line prints text to the console
System.out.println("Hello, World!");
}
}
Let's break this down. Every Java program is built around at least one class. Think of a class as a container for your code. Here, we've named our class HelloWorld.
The line public static void main(String[] args) is special. It's the main entrance to your program. When you run the code, the Java Virtual Machine (JVM) looks for this exact line to know where to start.
Inside the main method, we have the instruction: System.out.println("Hello, World!");. This is the command that does the work. System.out.println() is a built-in Java function that prints a line of text to the console. Notice the semicolon ; at the end. In Java, semicolons are like periods in a sentence; they mark the end of a complete statement.
Storing Information with Variables
Programs need to work with information, like numbers, text, or true/false values. To keep track of this information, we use variables. A variable is like a labeled box where you can store a piece of data. To create a variable, you must declare its type and give it a name.
The syntax is:
type variableName = value;
For example, to store the number of players in a game, you could write:
int numberOfPlayers = 4;
Here, int is the data type (for integer, or whole number), numberOfPlayers is the name we've given our variable, and 4 is the value we're storing in it. This process of giving a variable its first value is called initialization.
Java's Data Types
Java has two main categories of data types: primitive types and reference types. Understanding the difference is key to writing effective code.
Primitive types are the most basic building blocks. They store simple values directly in the variable's memory location.
There are eight primitive types in Java, but these four are the ones you'll use most often when you're starting out.
| Type | Description | Example |
|---|---|---|
int | Whole numbers | int score = 100; |
double | Numbers with decimal points | double price = 19.99; |
boolean | true or false values | boolean isGameOver = false; |
char | A single character | char grade = 'A'; |
Reference types, on the other hand, don't store the data directly. Instead, they store a memory address, or a reference, that points to where the actual data is located. The most common reference type you'll encounter is the String.
A
Stringis used to store a sequence of characters, like a name or a message.
String playerName = "Alex";
This diagram shows the conceptual difference in how a primitive int and a reference String store their data.
Input and Output
We've already seen how to output information using System.out.println(). This is great for showing results, but what about getting input from the user? To do that, we need to use a tool from Java's library called the Scanner.
import java.util.Scanner; // 1. Import the Scanner
public class UserInput {
public static void main(String[] args) {
// 2. Create a Scanner object
Scanner inputReader = new Scanner(System.in);
System.out.println("Enter your name: ");
// 3. Read the user's input
String userName = inputReader.nextLine();
// 4. Use the input in your output
System.out.println("Hello, " + userName + "!");
inputReader.close(); // 5. Close the scanner
}
}
Let's walk through the numbered steps in the code:
- Import: We first need to tell Java we want to use the
Scannerclass, which lives in thejava.utilpackage. - Create: We create a new
Scannerobject namedinputReaderand tell it to read fromSystem.in(the standard input, which is usually the keyboard). - Read: The
inputReader.nextLine()method waits for the user to type something and press Enter. It then reads the entire line of text and stores it in ouruserNamevariable. - Use: We can now use the
userNamevariable. Here, we combine it with other strings using the+operator to create a personalized greeting. - Close: It's good practice to close the scanner when you're done with it to free up resources.
What is the special method that serves as the entry point for every Java application?
In Java, a class acts as a container for your code.
That covers the essential building blocks. You now know how to structure a simple program, store data, and interact with the user.