1.
Introduction to Java
Developed by: James Gosling at Sun Microsystems (1995)
Type: Object-Oriented, platform-independent, high-level programming language.
Motto: “Write Once, Run Anywhere” (WORA)
Key Features
Object-Oriented
Platform Independent (via JVM)
Robust and Secure
Multithreaded
Portable
High Performance (via Just-In-Time compilation)
2. Java Architecture
JVM (Java Virtual Machine): Runs Java bytecode.
JRE (Java Runtime Environment): Contains JVM + libraries to run programs.
JDK (Java Development Kit): Contains JRE + tools for development (compiler,
debugger, etc.).
3. Basic Syntax
public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
Explanation:
class → Blueprint for objects.
public static void main(String[] args) → Entry point of any Java program.
[Link]() → Prints output.
4. Data Types
Primitive Data Types
Type Size Example
byte 1 byte 127
short 2 bytes 32000
int 4 bytes 100000
long 8 bytes 9999999999L
float 4 bytes 3.14f
double 8 bytes 3.14159
char 2 bytes 'A'
boolean 1 bit true/false
Non-Primitive Types
String, Arrays, Classes, Interfaces, etc.
5. Operators
Arithmetic: +, -, *, /, %
Relational: ==, !=, >, <, >=, <=
Logical: &&, ||, !
Assignment: =, +=, -=, etc.
Unary: ++, --
Ternary: condition ? expr1 : expr2
6. Control Statements
If / else if / else
Switch
Loops: for, while, do-while, for-each
Jump Statements: break, continue, return
7. Object-Oriented Programming (OOP)
Core Concepts:
1. Class – Blueprint for creating objects.
2. Object – Instance of a class.
3. Encapsulation – Binding data and methods.
4. Inheritance – Reusing code (extends keyword).
5. Polymorphism – Many forms (method overloading/overriding).
6. Abstraction – Hiding internal details (abstract classes/interfaces).
8. Constructors
Used to initialize objects.
Same name as class.
No return type.
class Student {
String name;
Student(String n) {
name = n;
}
}
9. Inheritance Example
class Animal {
void eat() { [Link]("eating..."); }
}
class Dog extends Animal {
void bark() { [Link]("barking..."); }
}
class Test {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
10. Polymorphism
Compile-time (Overloading)
void show(int a);
void show(String b);
Runtime (Overriding)
class Parent {
void show() { [Link]("Parent"); }
}
class Child extends Parent {
void show() { [Link]("Child"); }
}
11. Abstraction
Abstract class: can have abstract + concrete methods.
Interface: only abstract methods (from Java 8, can have default/static methods).
12. Exception Handling
Try–Catch–Finally Example:
try {
int a = 5 / 0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
} finally {
[Link]("Finally block executed");
}
13. Collections Framework
Interfaces: List, Set, Map, Queue
Classes: ArrayList, LinkedList, HashSet, HashMap, TreeMap, etc.
Example:
ArrayList<String> list = new ArrayList<>();
[Link]("Java");
[Link]("Python");
[Link](list);
14. Multithreading
class MyThread extends Thread {
public void run() {
[Link]("Thread running...");
}
}
public class TestThread {
public static void main(String[] args) {
MyThread t = new MyThread();
[Link]();
}
}
15. File Handling
import [Link].*;
class FileDemo {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("[Link]");
[Link]("Hello Java");
[Link]();
}
}
16. Java 8+ Features
Lambda Expressions
Streams API
Functional Interfaces
Optional Class
Date and Time API
17. Important Keywords
static, final, this, super, new, return, break, continue, try, catch, throw, throws,
package, import