3

Java – Notes

1. Introduction

  • Java is a high-level, object-oriented programming language.

  • Developed by Sun Microsystems (now Oracle) in 1995.

  • Platform independent → “Write Once, Run Anywhere” (WORA).

  • Runs on Java Virtual Machine (JVM).


2. Features of Java

  1. Simple – Easy syntax similar to C/C++.

  2. Object-Oriented – Supports classes, objects, inheritance, polymorphism.

  3. Platform Independent – Code runs on any OS with JVM.

  4. Secure – Bytecode runs in JVM sandbox.

  5. Robust – Strong memory management, exception handling.

  6. Multithreaded – Can execute multiple threads simultaneously.

  7. Dynamic – Supports dynamic memory allocation, dynamic linking.


3. Java Architecture

  1. Java Source Code (.java) → Written by developer

  2. Compiler (javac) → Converts .java to Bytecode (.class)

  3. JVM → Executes bytecode on any platform

Flow:

 
Source Code (.java) → Compiler → Bytecode (.class) → JVM → Run

4. Java Syntax

 
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
  • class – Blueprint of object

  • main() – Entry point of program

  • System.out.println() – Prints output


5. Data Types

  1. Primitive Data Types
    | Type | Size | Example |
    |———–|————|———|
    | byte | 1 byte | 10 |
    | short | 2 bytes | 100 |
    | int | 4 bytes | 1000 |
    | long | 8 bytes | 10000L |
    | float | 4 bytes | 10.5f |
    | double | 8 bytes | 20.5 |
    | char | 2 bytes | ‘A’ |
    | boolean | 1 bit | true |

  2. Non-Primitive Data Types

  • Strings, Arrays, Classes, Interfaces, Objects


6. Variables

  • Declaration: int age;

  • Initialization: age = 25;

  • Types:

    • Local Variable → Inside method

    • Instance Variable → Inside class but outside method

    • Static/Global Variable → Declared with static keyword


7. Operators

  1. Arithmetic: + - * / %

  2. Relational: == != > < >= <=

  3. Logical: && || !

  4. Assignment: = += -= *= /= %=

  5. Increment/Decrement: ++ --


8. Control Statements

1. Decision Making:

 
if(condition) { }
else if(condition) { }
else { }
switch(variable) { case: break; default: }

2. Loops:

 
for(initialization; condition; increment) { }
while(condition) { }
do { } while(condition);

3. Jump Statements:

  • break → Exit loop

  • continue → Skip current iteration

  • return → Exit method


9. Classes & Objects

  • Class – Template for objects

 
class Car {
String color;
int speed;

void drive() {
System.out.println(“Car is driving”);
}
}

  • Object – Instance of class

 
Car myCar = new Car();
myCar.color = "Red";
myCar.drive();

10. Methods

  • Function inside class.

 
returnType methodName(parameters) {
// code
}
  • Example:

 
int add(int a, int b) {
return a + b;
}

11. Constructors

  • Special method to initialize objects

 
class Car {
String color;
Car(String c) { color = c; }
}
Car myCar = new Car("Red");

12. Inheritance

  • Mechanism to inherit properties and methods from another class.

  • Syntax: class Child extends Parent { }

  • Types: Single, Multilevel, Hierarchical, Multiple (via interfaces)


13. Polymorphism

  • Ability of object to take multiple forms

  1. Compile-time (Method Overloading) – Same method name, different parameters

  2. Runtime (Method Overriding) – Subclass provides its own version of method


14. Encapsulation

  • Wrapping data and methods together

  • Use private variables + public getters/setters

 
class Student {
private int age;
public void setAge(int a) { age = a; }
public int getAge() { return age; }
}

15. Abstraction

  • Hiding internal details, showing only functionality

  1. Abstract Class

 
abstract class Animal {
abstract void sound();
}
  1. Interface

 
interface Animal {
void sound();
}

16. Exception Handling

  • Handling runtime errors

 
try {
int a = 5/0;
} catch(ArithmeticException e) {
System.out.println("Error: " + e);
} finally {
System.out.println("Always executed");
}

17. Packages

  • Group of related classes/interfaces

  • Example: import java.util.Scanner;


18. Java Collections

  • ArrayList, HashMap, HashSet, LinkedList, etc.

  • Used for storing, managing, and manipulating objects


19. Threads (Multithreading)

  • Thread – Lightweight process

  • Creating Thread

 
class MyThread extends Thread {
public void run() { System.out.println("Thread running"); }
}
  • Runnable Interface

 
class MyThread implements Runnable { public void run(){} }

20. File I/O

  • Reading & Writing files

 
import java.io.*;
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
br.close();

21. Important Notes

  • Java is Case Sensitive

  • Java main method signature must be:

 
public static void main(String[] args)
  • JVM → JRE → JDK hierarchy:

    • JDK (Java Development Kit) → tools to write & compile code

    • JRE (Java Runtime Environment) → JVM + libraries

    • JVM (Java Virtual Machine) → executes bytecode