Java Programming Language Explained — Complete Beginner's Guide
Java is a statically typed, object-oriented programming language that runs on the Java Virtual Machine (JVM), enabling cross-platform development through bytecode that runs anywhere.
What You’ll Learn
- How the JVM provides platform independence
- Object-oriented programming: classes, inheritance, interfaces
- Build tools: Maven and Gradle
- Collections framework, streams, and lambdas
- Exception handling with try-catch
Why It Matters
Java powers enterprise systems worldwide — Android apps, banking platforms, big data infrastructure (Hadoop, Spark), and web servers. Doda Browser’s backend services use Java for their reliability at scale. Understanding Java opens doors to enterprise development, Android programming, and system architecture roles that few other languages offer.
Learning Path
flowchart LR
A[Java Basics<br/>You are here] --> B[OOP: Classes & Inheritance]
B --> C[Collections & Generics]
C --> D[Streams & Lambdas]
D --> E[Build a REST API]
The JVM and Write Once, Run Anywhere
Java source code compiles to bytecode (.class files) that runs on the JVM. The same bytecode runs on Linux, Windows, macOS, and any device with a JVM implementation.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}javac HelloWorld.java # compiles to HelloWorld.class
java HelloWorld # runs on JVM
# Hello, Java!Object-Oriented Programming
Java is built around classes and objects. A class is a blueprint; an object is an instance.
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void speak() {
System.out.println(name + " makes a sound");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
}
@Override
void speak() {
System.out.println(name + " says Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog("Rex");
a.speak(); // Rex says Woof!
}
}Interfaces and Polymorphism
Interfaces define contracts that classes must implement.
interface Drawable {
void draw();
}
class Circle implements Drawable {
public void draw() {
System.out.println("Drawing a circle");
}
}
class Square implements Drawable {
public void draw() {
System.out.println("Drawing a square");
}
}
public class Main {
public static void main(String[] args) {
Drawable[] shapes = {new Circle(), new Square()};
for (Drawable d : shapes) {
d.draw();
}
// Drawing a circle
// Drawing a square
}
}Collections Framework
Java provides ready-to-use data structures: List, Set, Map, and more.
import java.util.*;
public class CollectionsExample {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
fruits.add("Apple"); // duplicates allowed
System.out.println("Fruits: " + fruits);
// Fruits: [Apple, Banana, Cherry, Apple]
Set<Integer> numbers = new HashSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(1); // duplicate ignored
System.out.println("Numbers: " + numbers);
// Numbers: [1, 2]
Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 95);
scores.put("Bob", 87);
System.out.println("Alice's score: " + scores.get("Alice"));
// Alice's score: 95
}
}Streams and Lambdas
Java 8 introduced functional programming with streams and lambda expressions.
import java.util.*;
import java.util.stream.*;
public class StreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> evenSquares = numbers.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.collect(Collectors.toList());
System.out.println(evenSquares);
// [4, 16, 36]
int sum = numbers.stream()
.reduce(0, Integer::sum);
System.out.println("Sum: " + sum);
// Sum: 21
}
}Maven and Gradle
Maven and Gradle are build tools that manage dependencies, compile code, run tests, and package artifacts.
<!-- pom.xml (Maven) -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>// build.gradle (Gradle)
dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
}Both tools download dependencies from repositories like Maven Central. Gradle is faster and uses a Groovy or Kotlin DSL; Maven uses XML and is more widely adopted in enterprises.
Exception Handling
Java uses checked exceptions (must be handled) and unchecked exceptions (runtime).
import java.io.*;
public class ExceptionExample {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(
new FileReader("config.txt")
);
String line = reader.readLine();
System.out.println("Config: " + line);
reader.close();
} catch (FileNotFoundException e) {
System.out.println("Config file not found, using defaults");
} catch (IOException e) {
System.out.println("Error reading config: " + e.getMessage());
} finally {
System.out.println("Cleanup complete");
}
}
}Common Mistakes
1. Confusing == with .equals()
== compares reference identity, not value. Always use .equals() for string comparison.
String a = new String("hello");
String b = new String("hello");
System.out.println(a == b); // false (different objects)
System.out.println(a.equals(b)); // true (same value)2. Forgetting to close resources
Files, streams, and database connections must be closed. Use try-with-resources (Java 7+).
3. NullPointerException from uninitialized objects
Always initialize fields or check for null before calling methods.
4. Using raw types instead of generics
List list = new ArrayList(); // raw type — unchecked warnings
List<String> list = new ArrayList<>(); // proper generic5. Catching Exception too broadly
Catching Exception hides bugs. Catch specific exception types and handle each appropriately.
6. Modifying a collection while iterating
Use Iterator.remove() or collect results in a new list instead.
Practice Questions
What is the JVM? The Java Virtual Machine executes Java bytecode. It provides memory management, JIT compilation, and cross-platform portability.
What’s the difference between
abstract classandinterface? Abstract classes can have state and constructors; interfaces define contracts. A class can extend one abstract class but implement multiple interfaces.How does
try-with-resourceswork? Resources declared in the try block are automatically closed when the block exits, even on exceptions.What is a lambda expression? A concise way to implement a functional interface.
(x, y) -> x + yis a lambda.What does Maven’s
pom.xmldo? It defines project metadata, dependencies, plugins, and build configuration. Maven uses it to compile, test, and package your project.
Challenge: Write a Java program that reads a text file, counts word frequencies using a HashMap, and prints the top 10 most common words.
Mini Project — Todo List CLI
Build a command-line todo list manager using Java collections.
import java.util.*;
public class TodoList {
private List<String> todos = new ArrayList<>();
private Scanner scanner = new Scanner(System.in);
public void run() {
while (true) {
System.out.println("\n--- Todo List ---");
System.out.println("1. Add task");
System.out.println("2. List tasks");
System.out.println("3. Remove task");
System.out.println("4. Quit");
System.out.print("Choose: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
System.out.print("Enter task: ");
todos.add(scanner.nextLine());
break;
case 2:
for (int i = 0; i < todos.size(); i++) {
System.out.println((i + 1) + ". " + todos.get(i));
}
break;
case 3:
System.out.print("Enter number to remove: ");
int idx = scanner.nextInt() - 1;
if (idx >= 0 && idx < todos.size()) {
todos.remove(idx);
}
break;
case 4:
return;
}
}
}
public static void main(String[] args) {
new TodoList().run();
}
}FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro