Java: The Complete Beginner-to-Advanced Guide (With Practical Code Examples)
Source: Dev.to
What Is Java and Why Is It Popular?
Java is a class‑based, object‑oriented, platform‑independent, strongly typed programming language.
Key Features
- Platform Independent (“Write Once, Run Anywhere” – WORA)
- Object‑Oriented (OOP: classes, objects, inheritance, polymorphism)
- Multi‑threaded
- Secure & Robust
- Huge Ecosystem (Spring Boot, Maven, Gradle, JavaFX)
- Massive Community Support
Setting Up Java
Install Java
Download the JDK from:
Check version
java -version
javac -version
Your First Java Program
Create a file named Main.java:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
Compile and run:
javac Main.java
java Main
Variables and Data Types
Java is strongly typed, so every variable must have a type.
public class Main {
public static void main(String[] args) {
int age = 25;
double price = 99.99;
char grade = 'A';
boolean isActive = true;
String name = "Farhad";
System.out.println(name + " - " + age);
}
}
Control Flow Statements
If / Else
int score = 80;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 50) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Switch
String day = "Monday";
switch (day) {
case "Monday":
System.out.println("Start of week");
break;
default:
System.out.println("Unknown day");
}
Loops
for (int i = 1; i names = new ArrayList<>();
names.add("Farhad");
names.add("Rahimi");
System.out.println(names);
}
}
Object‑Oriented Programming (OOP)
Class and Object
class Car {
String model;
int year;
void start() {
System.out.println(model + " is starting...");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.model = "Toyota";
c.year = 2024;
c.start();
}
}
Constructors
class User {
String name;
int age;
User(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
User u = new User("Farhad", 23);
System.out.println(u.name);
}
}
Inheritance
class Animal {
void sound() {
System.out.println("Makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
Polymorphism
class Shape {
void draw() {
System.out.println("Drawing shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing circle");
}
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle();
s.draw();
}
}
Encapsulation
class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount();
acc.deposit(100);
System.out.println(acc.getBalance());
}
}
Abstraction (Interfaces & Abstract Classes)
Interface
interface Animal {
void eat();
}
class Cat implements Animal {
public void eat() {
System.out.println("Cat eating");
}
}
Abstract Class
abstract class Vehicle {
abstract void move();
}
class Bike extends Vehicle {
void move() {
System.out.println("Bike moves fast");
}
}
Exception Handling
try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Always runs");
}
File Handling
import java.io.FileWriter;
public class Main {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("output.txt");
fw.write("Java file writing...");
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java and OOP in Real Projects
Java is widely used in:
- Spring Boot (Enterprise Backends)
- Android Development
- Financial Services
- Large‑scale Microservices
- Distributed Systems
- Big Data with Hadoop
- Cloud Applications (AWS, GCP, Azure)
Most production environments prefer Java due to its reliability and scalability.
A Mini Java Project: Simple To‑Do Console App
import java.util.ArrayList;
import java.util.Scanner;
public class TodoApp {
public static void main(String[] args) {
ArrayList tasks = new ArrayList<>();
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("1. Add Task 2. View Tasks 3. Exit");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
System.out.print("Enter task: ");
tasks.add(sc.nextLine());
break;
case 2:
System.out.println("Your tasks:");
for (String t : tasks) System.out.println("- " + t);
break;
case 3:
System.exit(0);
default:
System.out.println("Invalid choice");
}
}
}
}
Conclusion
Java remains one of the most powerful languages for backend development, enterprise systems, Android applications, and scalable architectures. Its mature ecosystem, strong community, and long‑term stability make it a top choice for developers worldwide.
Whether you’re building microservices with Spring Boot, high‑performance servers, or cross‑platform applications, Java gives you the tools to succeed.