UNIT – 1 : INTRODUCTION TO JAVA PROGRAMMING
1. BENEFITS OF OBJECT-ORIENTED PROGRAMMING (OOPs)
Object-Oriented Programming (OOP) focuses on real-world objects and data rather than
logic.
Main Benefits: • Reusability – Code can be reused using inheritance. • Data Security –
Achieved through encapsulation. • Modularity – Divides program into independent parts. •
Flexibility – Easy modification and maintenance. • Polymorphism – One interface, multiple
forms. • Extensibility – Easy to add new features. • Efficiency – Reduces redundancy.
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]();
}
}
2. HISTORY OF JAVA
• Developed by James Gosling at Sun Microsystems (1991). • Originally called “Oak”,
renamed “Java” in 1995. • Motto: “Write Once, Run Anywhere (WORA)”
Timeline: 1991 – Project Green started 1995 – Java 1.0 released 1998 – Java 2
introduced (J2SE, J2EE, J2ME) 2006 – Became Open Source 2010 – Acquired by Oracle
Java Editions: • Java SE – Standard Edition (Desktop) • Java EE – Enterprise Edition
(Web) • Java ME – Micro Edition (Mobile) • JavaFX – GUI applications
3. FEATURES OF JAVA
• Simple – Easy to learn, no pointers. • Object-Oriented – Everything is an object. •
Platform Independent – Runs anywhere using JVM. • Secure – No manual memory
access. • Robust – Strong memory management, exception handling. • Multithreaded –
Supports concurrent execution. • Portable – Platform-independent bytecode. • Dynamic –
Links classes dynamically. • High Performance – JIT compiler improves speed.
4. JAVA ENVIRONMENT
Java Environment includes:
• JDK (Java Development Kit): Tools + Compiler + JRE. • JRE (Java Runtime
Environment): JVM + Libraries. • JVM (Java Virtual Machine): Executes bytecode.
Execution Process: 1. Write program ([Link]) 2. Compile → javac [Link] 3. Run →
java Hello
Example:
class Hello {
public static void main(String[] args) {
[Link]("Hello Java!");
}
}
5. JAVA TOKENS
Tokens are smallest individual units of a program.
Types: • Keywords → class, public, static, void • Identifiers → names for
variables/methods • Literals → fixed values like 10, 'A', "Hello" • Operators → +, -, *, /, % •
Separators → (), {}, [], ;, ,
6. CONSTANTS
Constants are fixed values that don’t change.
Example:
final double PI = 3.14159;
Types: • Integer • Floating-point • Character • String • Boolean
7. VARIABLES
A variable is a named memory location used to store values.
Types: • Local – Declared inside a method. • Instance – Declared in class but outside
methods. • Static – Declared with 'static', shared among objects.
Example:
class Example {
static int count = 0;
int id;
void show() {
int num = 10;
[Link](num);
}
}
8. DATA TYPES
Primitive Data Types: byte, short, int, long, float, double, char, boolean
Example:
int a = 10;
float b = 5.5f;
Non-Primitive Types: String, Array, Class, Interface
Example:
String name = "Java";
9. OPERATORS AND EXPRESSIONS
Operators are symbols that perform operations.
Types: • Arithmetic → +, -, *, /, % • Relational → >, <, ==, != • Logical → &&, ||, ! •
Assignment → =, +=, -= • Increment/Decrement → ++, -- • Conditional → ?: • Bitwise → &,
|, ^, <<, >>
Example:
int a = 10, b = 20;
int max = (a > b) ? a : b;
10. DECISION MAKING AND BRANCHING
Used to control program flow.
if
Example:
if (a > b)
[Link]("A is greater");
else
[Link]("B is greater");
switch
Example:
int day = 2;
switch(day) {
case 1: [Link]("Monday"); break;
default: [Link]("Other day");
}
11. DECISION MAKING AND LOOPING
Used to execute code repeatedly.
while Loop: int i = 1; while(i <= 5) { [Link](i); i++; }
do-while Loop: int i = 1; do { [Link](i); i++; } while(i <= 5);
for Loop: for(int i = 1; i <= 5; i++) [Link](i);
Jump Statements: break – exits loop continue – skips current iteration
Example:
for(int i = 1; i <= 5; i++) {
if(i == 3) continue;
[Link](i);
}