Java Backend Development Essentials
Java Basics
The Building Blocks of Java
Every programming language has its own grammar, or syntax. In Java, instructions are written as statements, and each statement typically ends with a semicolon. It's like the period at the end of a sentence. A basic Java program is organized within a class, and the starting point for execution is a special method called main.
public class HelloWorld {
// This is the main method, the program's entry point.
public static void main(String[] args) {
// This line prints "Hello, World!" to the console.
System.out.println("Hello, World!");
}
}
To get any real work done, we need to store information. We do this using variables. A variable is just a named container for a piece of data. When you create a variable in Java, you must declare its data type, which tells the computer what kind of information it will hold.
Java has several fundamental, or primitive, data types for storing simple values. There are also more complex types, like String, which is used for text.
| 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'; |
String | A sequence of characters | String name = "Alex"; |
Making Decisions and Repeating Actions
Programs rarely run straight through from top to bottom. They need to make decisions and perform repetitive tasks. This is where control structures come in. Conditional statements, like if-else, let your program execute different blocks of code based on whether a condition is true or false.
int score = 85;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 70) {
System.out.println("Good job.");
} else {
System.out.println("Keep trying!");
}
// Prints: Good job.
Loops are used to repeat a block of code multiple times. The for loop is great when you know exactly how many times you want to iterate, like counting from 1 to 5.
// This loop prints the numbers 1 through 5.
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
The while loop is better suited for situations where you want to keep repeating an action as long as a certain condition remains true, but you don't know ahead of time how many repetitions that will be.
int batteryLevel = 100;
while (batteryLevel > 0) {
System.out.println("Using device. Battery: " + batteryLevel);
batteryLevel = batteryLevel - 10;
}
System.out.println("Battery dead!");
Thinking in Objects
Java is an object-oriented programming (OOP) language. This is a way of designing programs by modeling real-world things as objects. Think about a car. A car has properties (like color and speed) and behaviors (like starting the engine and braking). In OOP, we bundle these properties and behaviors together into a single unit.
The blueprint for creating an object is called a class. A class defines the properties (variables) and behaviors (methods) that all objects of that type will have. An object is a specific instance created from that class blueprint. You can have one Car class, but many Car objects, each with its own color and current speed.
// This is the blueprint (class) for a Car.
public class Car {
// Properties (variables)
String color;
int speed;
// Behaviors (methods)
void startEngine() {
System.out.println("Engine started.");
}
void accelerate() {
speed = speed + 10;
}
}
This leads to four core principles of OOP.
1. Encapsulation
Encapsulation means bundling an object's data and the methods that operate on that data into one unit. It also involves hiding the object's internal state to protect it from outside interference. We use access modifiers like private to prevent other parts of the code from directly changing an object's properties. This forces interaction to happen through designated methods, giving you more control.
encapsulation
noun
The bundling of data with the methods that operate on that data, and the restriction of direct access to some of an object's components.
2. Abstraction
Abstraction means hiding complex implementation details and showing only the essential features of an object. When you drive a car, you use the steering wheel and pedals. You don't need to know the complex mechanics of the engine or transmission to make it go. Abstraction in code works the same way, providing a simple interface to complex functionality.
3. Inheritance
Inheritance allows a new class to be based on an existing class. The new class, called a subclass, inherits the properties and methods of the existing class, known as the superclass. This promotes code reuse. For example, an ElectricCar class could inherit from our Car class. It would automatically have a color and speed, but we could add properties and methods specific to electric cars, like batteryCharge.
// ElectricCar inherits from Car
public class ElectricCar extends Car {
int batteryCharge;
void charge() {
System.out.println("Charging...");
batteryCharge = 100;
}
}
4. Polymorphism
Polymorphism, which means "many forms," allows an object to be treated as an instance of its parent class. This means we can perform a single action in different ways. For instance, both a Car and an ElectricCar can startEngine(), but they might do it differently. The ElectricCar version might be silent, while the standard Car makes a noise. Polymorphism lets the program figure out which specific version of the method to run at the right time.
Let's test your understanding of these core Java concepts.
In Java, what punctuation mark is typically used to end a statement?
Which control structure is most appropriate when you want to execute a block of code a specific number of times, for example, exactly 10 times?
These fundamentals are the bedrock of Java development. Mastering them allows you to build organized, efficient, and scalable applications.