Java Programming Guide
A 10-page beginner + intermediate guide to Java.
Introduction & History of Java
Java was developed by James Gosling at Sun Microsystems in 1995.
It is an object-oriented, platform-independent programming language known for its
portability and security.
Features of Java
- Platform Independent
- Object Oriented
- Simple and Secure
- Robust
- Multithreaded
- High Performance (JIT Compiler)
- Distributed
Java Architecture (JVM, JRE, JDK)
- JVM (Java Virtual Machine): Executes bytecode.
- JRE (Java Runtime Environment): Contains JVM + libraries.
- JDK (Java Development Kit): Contains JRE + development tools (compiler, debugger).
Basic Syntax & Data Types
Java is case-sensitive. Every program starts with the 'main' method.
Data Types: int, float, double, char, boolean, String.
Example:
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
OOP Concepts
- Encapsulation: Wrapping data and methods together.
- Inheritance: One class inherits from another.
- Polymorphism: Ability of an object to take many forms.
- Abstraction: Hiding implementation details, exposing functionality.
Exception Handling
Java uses try, catch, finally, and throw.
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Error: Division by zero");
}
Collections Framework
Java provides classes like List, Set, Map.
Example:
import [Link].*;
List<String> list = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
[Link](list);
Multithreading Basics
Java supports multithreading using Thread class and Runnable interface.
Example:
class MyThread extends Thread {
public void run() {
[Link]("Thread is running...");
public class Main {
public static void main(String args[]) {
MyThread t1 = new MyThread();
[Link]();
}
Memory Management & Garbage Collection
Java automatically manages memory using Garbage Collector (GC).
Objects with no references are cleared from memory.
Sample Programs
1. Fibonacci Series
2. Prime Number Check
3. File Handling Example
Example Fibonacci:
public class Fibonacci {
public static void main(String[] args) {
int n1=0,n2=1,n3,i,count=10;
[Link](n1+" "+n2);
for(i=2;i<count;++i){
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;