Java Programming: Core Concepts & Practical Code Snippets
Overview: This post walks through key Java concepts with short, runnable code snippets. It's ideal for beginners moving to intermediate Java: object-oriented design, collections, exception handling, and file I/O.
| first | second | third | foruth |
| first | first | first | first |
| first | first | firstfirst | first |
| first | first | first | first |
1. Hello World & Basic Syntax
A minimal Java program and how to compile/run it.
// Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Compile: javac Hello.java · Run: java Hello
2. Variables, Types & Control Flow
int a = 10;
double price = 19.99;
boolean active = true;
if (a > 5) {
System.out.println("a is greater than 5");
} else {
System.out.println("a is 5 or less");
}
// For loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
3. Object-Oriented Programming (Classes & Inheritance)
Define classes, constructors, and extend behavior.
// Vehicle.java
public class Vehicle {
protected String brand;
public Vehicle(String brand) {
this.brand = brand;
}
public void info() {
System.out.println("Brand: " + brand);
}
}
// Car.java
public class Car extends Vehicle {
private int doors;
public Car(String brand, int doors) {
super(brand);
this.doors = doors;
}
@Override
public void info() {
super.info();
System.out.println("Doors: " + doors);
}
}
// Usage
Car c = new Car("Toyota", 4);
c.info();
4. Interfaces & Polymorphism
interface Shape {
double area();
}
class Circle implements Shape {
private double r;
Circle(double r) { this.r = r; }
@Override
public double area() { return Math.PI * r * r; }
}
class Rectangle implements Shape {
private double w, h;
Rectangle(double w, double h) { this.w = w; this.h = h; }
@Override
public double area() { return w * h; }
}
// Usage
Shape s1 = new Circle(2.0);
Shape s2 = new Rectangle(3.0, 4.0);
System.out.println(s1.area());
System.out.println(s2.area());
5. Collections: List, Set, Map
import java.util.*;
List list = new ArrayList<>();
list.add("apple");
list.add("banana");
Set set = new HashSet<>(list);
set.add("apple"); // duplicate ignored
Map counts = new HashMap<>();
for (String s : list) {
counts.put(s, counts.getOrDefault(s, 0) + 1);
}
System.out.println(list);
System.out.println(set);
System.out.println(counts);
6. Exception Handling
try {
int x = Integer.parseInt("abc"); // NumberFormatException
} catch (NumberFormatException e) {
System.err.println("Invalid number: " + e.getMessage());
} finally {
System.out.println("Cleanup if needed");
}
7. File I/O (try-with-resources)
import java.io.*;
import java.nio.file.*;
Path path = Paths.get("example.txt");
try (BufferedWriter writer = Files.newBufferedWriter(path)) {
writer.write("Hello from Java!");
} catch (IOException e) {
e.printStackTrace();
}
// Read file
try (BufferedReader reader = Files.newBufferedReader(path)) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
8. Simple Multi-threading
class Worker implements Runnable {
private final int id;
Worker(int id) { this.id = id; }
public void run() {
System.out.println("Worker " + id + " started");
try { Thread.sleep(500); } catch (InterruptedException ignored) {}
System.out.println("Worker " + id + " finished");
}
}
// Usage
Thread t1 = new Thread(new Worker(1));
Thread t2 = new Thread(new Worker(2));
t1.start();
t2.start();
9. Closing Notes & Next Steps
This post covered practical Java building blocks. Next, try building a small project — a CLI todo app or a file-processing tool — to combine these concepts. For deeper learning, explore generics, streams & lambdas, and unit testing with JUnit.