Java Short Notes
Java is a popular, object-oriented programming language that is designed to be platform-
independent. It’s widely used for developing web applications, mobile applications (Android),
and large-scale enterprise systems.
1. Introduction to Java
Java is a high-level, class-based, object-oriented programming language designed to
have as few implementation dependencies as possible.
Key Features:
o Platform Independence: "Write Once, Run Anywhere" (WORA) philosophy.
o Object-Oriented: Everything in Java is treated as an object.
o Memory Management: Automatic garbage collection.
o Multithreading: Supports multiple threads of execution within a program.
o Rich API: Large standard library for networking, utilities, data structures, and
more.
o Security: Strong security features like bytecode verification and security
managers.
2. Java Basics
2.1. Java Syntax
Class Declaration: All Java programs are written inside classes.
class MyClass {
// Code goes here
}
Main Method: Entry point for Java programs.
public class Main {
public static void main(String[] args) {
[Link]("Hello, World!");
}
}
2.2. Variables and Data Types
Primitive Data Types: byte, short, int, long, float, double, char, boolean.
Reference Types: Arrays, Objects.
Example:
int age = 25; // primitive
String name = "John"; // reference
2.3. Control Structures
If-Else:
if (x > 10) {
// do something
} else {
// do something else
}
Switch-Case:
switch (day) {
case 1: [Link]("Monday"); break;
case 2: [Link]("Tuesday"); break;
default: [Link]("Invalid day");
}
3. Object-Oriented Programming (OOP) in Java
3.1. Classes and Objects
Class: Blueprint for creating objects.
Object: Instance of a class.
class Dog {
String breed;
int age;
void bark() {
[Link]("Woof!");
}
}
Object Creation:
Dog myDog = new Dog();
[Link]();
3.2. Encapsulation
Encapsulation is the technique of wrapping data (variables) and methods into a single
unit called a class.
class Person {
private String name; // private variable
public String getName() {
return name; // getter method
}
public void setName(String name) {
[Link] = name; // setter method
}
}
3.3. Inheritance
Inheritance allows one class to inherit fields and methods from another class.
class Animal {
void eat() {
[Link]("Eating...");
}
}
class Dog extends Animal {
void bark() {
[Link]("Barking...");
}
}
3.4. Polymorphism
Polymorphism allows methods to do different things based on the object they are acting
upon.
class Animal {
void sound() {
[Link]("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
[Link]("Bark");
}
}
3.5. Abstraction
Abstraction hides the complex implementation details and shows only the essential
features.
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
[Link]("Bark");
}
}
3.6. Interfaces
An interface defines a contract of methods that a class must implement.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
[Link]("Bark");
}
}
4. Exception Handling
Purpose: Handle runtime errors so that the program can continue executing.
Try-Catch Block:
try {
int result = 10 / 0; // ArithmeticException
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
}
Finally Block: Code that always runs after try-catch block.
try {
// code
} catch (Exception e) {
// error handling
} finally {
// cleanup code
}
5. Collections Framework
List: Ordered collection (e.g., ArrayList, LinkedList).
Set: Collection that does not allow duplicates (e.g., HashSet).
Map: Collection of key-value pairs (e.g., HashMap).
Example:
import [Link].*;
public class Example {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
[Link]("Apple");
[Link]("Banana");
for (String fruit : list) {
[Link](fruit);
6. Multithreading
Thread: A lightweight process that allows concurrent execution of code.
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](); // Start the thread
}
}
Synchronization: Ensures that multiple threads do not conflict when accessing shared
resources.
synchronized void syncMethod() {
// synchronized block of code
}
7. Java Memory Management
Heap: Used for dynamic memory allocation (objects).
Stack: Used for method calls and local variables.
Garbage Collection: Automatic process to remove unused objects from memory.
8. Java I/O
File I/O: Reading and writing files using classes like FileReader, BufferedReader, and
FileWriter.
import [Link].*;
public class FileExample {
public static void main(String[] args) throws IOException {
FileWriter writer = new FileWriter("[Link]");
[Link]("Hello, Java!");
[Link]();
}
}
9. Java 8 Features
Lambda Expressions: Anonymous function that can be used to implement functional
interfaces.
(int a, int b) -> a + b;
Streams API: Allows processing sequences of elements, like collections, in a functional
way.
List<Integer> numbers = [Link](1, 2, 3, 4, 5);
[Link]().filter(n -> n > 2).forEach([Link]::println);
10. Java Virtual Machine (JVM)
JVM is the engine that executes Java bytecode, providing platform independence.
Key components:
o Class Loader: Loads class files.
o Bytecode Verifier: Ensures code safety.
o Execution Engine: Executes bytecode.
11. Java Tools and Frameworks
Maven/Gradle: Build tools for managing dependencies and building Java projects.
Spring Framework: A powerful framework for building enterprise applications.
Hibernate: ORM (Object-Relational Mapping) framework for database interaction.