Introduction to Java
Java is a high-level, object-oriented, platform-independent programming
language developed by Sun Microsystems (now owned by Oracle) in
1995.
It is widely used for building applications ranging from desktop software
to web, mobile (especially Android), and enterprise systems.
Basic Concepts of Object Oriented
Programming
1)Classes
2)Objects
3)Data Abstraction and Encapsulation
4)Inheritance
5Polymorphism
6)Dynamic Binding
7)Message Pagging
Define class in Java
• In Java, a class is a blueprint or template used to create objects.
• It defines the properties (data/variables) and behaviors (methods/functions) that the
objects created from the class will have.
• Key Points about a Class:
• Definition:
• A class is a user-defined data type that groups fields (variables) and methods into a single
unit.
• Object-Oriented:
• Since Java is an object-oriented programming language, classes are the building blocks.
Objects are instances of classes.
• Syntax:
• class ClassName {
• // fields (variables)
• int x;
• String name;
• // methods (functions)
• void display() {
• [Link]("Name: " + name + ", X: " + x);
• }
• }
Example
class ClassName {
// fields (variables)
int x;
String name;
// methods (functions)
void display() {
[Link]("Name: " + name + ", X: " + x);
}
}
Program
public class Main {
public static void main(String[] args) {
// creating object of ClassName
ClassName obj = new ClassName();
// assigning values
obj.x = 10;
[Link] = "Java";
// calling method
[Link]();
}
}
Output
• Name: Java, X: 10
--------------------------
Example in Real Life:
Class → "Car"
Objects → "Honda", "BMW", "Toyota"
Fields → color, speed, price
Methods → start(), stop(), accelerate()
So, a class is just like a design/blueprint, and objects are the real-world entities created from that design.
Define Object
• In Java, an object is a real-world entity or an instance of a class.
• A class is just a blueprint, but an object is the actual thing created from that
blueprint. Each object has its own copy of data (fields/variables) and can use
the methods defined in the class.
• Definition:
• 👉 An object is a software bundle that contains:
• State (attributes/fields/variables) → represents the data.
• Behavior (methods/functions) → represents the actions the object can
perform.
Ex class and method creation
class Car {
// fields
String color;
int speed;
// method
void drive() {
[Link](color + " car is driving at " + speed + " km/h");
}
}
main method and object creation
public class Main {
public static void main(String[] args) {
// creating objects
Car car1 = new Car();
Car car2 = new Car();
// assigning values
[Link] = "Red";
[Link] = 120;
[Link] = "Blue";
[Link] = 100;
// calling methods
[Link]();
[Link]();
}
}
output
Red car is driving at 120 km/h
Blue car is driving at 100 km/h
in short
• Class = Definition / Blueprint
• Object = Instance of the class / Real entity created from the blueprint
Data Abstraction in Java
Definition:
• Data Abstraction is the process of hiding internal implementation details of a class and only showing the
essential features to the user.
• It focuses on what an object does rather than how it does it.
• Example in Real Life:
• When you drive a car, you only know how to use the steering wheel, accelerator, and brake.
• You don’t need to know the complex details of how the engine, gears, or fuel injection works internally.
• This is abstraction: you see only the important part (driving functions), while unnecessary details are hidden.
In short
• Abstraction = Hiding "How it works" and showing only "What it does".
• Topic will taken later...when u learn abstract keyword
Inheritance in Java
Definition:
• Inheritance is an object-oriented programming (OOP) feature in Java
that allows one class to acquire the properties (fields) and behaviors
(methods) of another class.
• It helps in code reusability and represents an “IS-A” relationship.
Key Terms
• Parent class / Superclass → The class whose properties are inherited.
• Child class / Subclass → The class that inherits properties from the
parent.
• Keyword used → extends.
• class B extends A
Example of inheritance
class Animal {
void eat() {
[Link]("This animal eats food.");
}
}
// Child class
class Dog extends Animal {
void bark() {
[Link]("The dog barks.");
}
}
Main Class
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link](); // Inherited method
[Link](); // Own method
}
}
• Inheritance = One class reusing another class’s code (IS-A
relationship).
• Advantage
• Code reusability
Polymorphism
• Definition:
• The word Polymorphism comes from Greek: poly = "many", morph =
"forms".
• In Java, Polymorphism means one entity (method, object, or
operator) can perform different actions in different situations.
• It allows the same method or object to behave differently depending
on the context.
• Polymorphism = Same name, different behaviors (many forms).
Ex
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
[Link]([Link](5, 10)); // calls int version
[Link]([Link](5.5, 4.5)); // calls double version
}
}
output
15
10.0
Dynamic Binding in Java
• Definition:
• Dynamic Binding (Late Binding / Runtime Binding) is the process
where the method to be called is determined at runtime, not at
compile time.
• It usually happens in method overriding when a superclass reference
points to a subclass object.
Message Passing
Definition:
• In Object-Oriented Programming (OOP), message passing is the
process by which objects communicate with each other by invoking
methods.
• When one object wants another object to perform some task, it sends
a message (in Java, this means calling a method on that object).
• How It Works in Java
• An object requests another object to execute one of its methods.
• The request is like sending a message to that object.
• The receiving object processes the message and may return a response.
• In Java, method calls represent message passing.
Ex
class Calculator {
int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
// Object creation
Calculator calc = new Calculator();
// Message passing: calling 'add' method
int result = [Link](10, 20);
[Link]("Sum = " + result);
}
}
0utput
• [Link](10, 20) → is message passing.
• calc receives the message add(10, 20) and responds by returning 30.
Features of Java
• Key Features of Java:
• Platform Independent – Programs written in Java can run on any system that has the Java Virtual Machine (JVM),
following the principle “Write Once, Run Anywhere” (WORA).
• Object-Oriented – Supports concepts like classes, objects, inheritance, polymorphism, and encapsulation.
• Robust and Secure – Has strong memory management, automatic garbage collection, and built-in security
features.
• Multithreading – Supports executing multiple tasks simultaneously.
• Portable – Java programs are compiled into bytecode, which can run on any device with JVM.
• Rich API and Libraries – Provides built-in libraries for networking, data structures, GUI development, and more.
• JDK stands for Java Development Kit.
• It is a software development environment used to develop Java applications and applets. JDK is provided by
Oracle and other vendors.
• Key points about JDK:
• Contains tools for development → compilers, debuggers, and other utilities.
• Includes JRE (Java Runtime Environment) → so you can also run Java programs.
• Has the JVM (Java Virtual Machine) → the engine that executes Java bytecode.
• Provides libraries (APIs) for building Java programs.
Components of JDK:
• JVM (Java Virtual Machine) – Executes Java bytecode.
• JRE (Java Runtime Environment) – Contains JVM + libraries required to run Java.
• Compiler (javac) – Converts Java source code (.java) into bytecode (.class).
• Development tools – such as javadoc, jdb (debugger), javap (disassembler).
• 👉 In short:
• JDK = JRE + Development tools
• JRE = JVM + Libraries
• JVM stands for Java Virtual Machine.
• It is the engine that runs Java programs by converting the compiled Java bytecode (.class files) into machine code (instructions
understood by your computer’s processor).
• Key Points about JVM:
• Platform Independent: Java code is written once and can run anywhere because JVM translates the same bytecode to the native
machine instructions of the system (Windows, Linux, Mac, etc.).
• Part of JRE: JVM is included inside the Java Runtime Environment (JRE).
• Execution Role: It loads, verifies, and executes Java bytecode.
• Memory Management: Handles memory through the Garbage Collector, which automatically frees unused memory.
• Steps of Execution in JVM:
• Class Loader → Loads .class files into memory.
• Bytecode Verifier → Checks code for security and correctness.
• Interpreter / JIT Compiler → Converts bytecode into machine code.
• Execution Engine → Runs the program instructions on hardware
• VM stands for Java Virtual Machine.
• It is a software-based engine that provides a runtime environment for executing Java programs.
The JVM is responsible for converting the compiled Java bytecode (from .class files) into
machine code that your operating system and hardware can understand.
• Key Points about JVM:
• Part of JRE – JVM is included inside the Java Runtime Environment (JRE).
• Platform-independent execution – Because Java code is compiled into bytecode, the same
program can run on any machine that has a JVM installed (this is what makes Java platform-
independent).
JVM
• JVM stands for Java Virtual Machine.
• It is a software-based engine that provides a runtime environment for
executing Java programs. The JVM is responsible for converting the
compiled Java bytecode (from .class files) into machine code that your
operating system and hardware can understand
Key Points about JVM:
• Part of JRE – JVM is included inside the Java Runtime Environment (JRE).
• Platform-independent execution – Because Java code is compiled into bytecode, the same program can run on any machine that has a JVM installed
(this is what makes Java platform-independent).
• Responsibilities of JVM:
• Loads code (Class Loader).
• Verifies code (Bytecode Verifier).
• Executes code (Interpreter / Just-In-Time Compiler).
• Provides runtime environment (memory management, garbage collection, security, etc.).
• Not a physical machine – It's a virtual machine (a program) that behaves like a machine for Java programs
JVM Architecture (Main
Components):
• Class Loader → Loads class files.
• Method Area → Stores class structures (metadata, code, static variables).
• Heap → Stores objects.
• Stack → Stores method calls and local variables.
• PC Register → Keeps track of the current instruction.
• Execution Engine → Executes bytecode (interpreter + JIT compiler).
• Garbage Collector → Frees unused memory automatically.
Datatypes
• In Java, data types specify the kind of values a variable can store and
how much memory it will take.
• Java has two main categories of data types:
• Primitive Data Types
• These are the basic built-in types in Java (8 in total).
Datatypes
Datatype Size Description Example
byte 1 byte (8-bit) Stores small integers (-128 to byte b = 100;
127).
short 2 bytes (16-bit) Larger range than byte (-32,768 short s = 3000
to 32,767).
int 4 bytes (32-bit) Commonly used for integers (- int x = 100000;
2³¹ to 2³¹-1).
long 8 bytes (64-bit) Very large integers. Ends with L. int x=1000000000L;
float 4 bytes Stores decimal numbers with float f = 3.14f;
single precision. Ends with f.
double 8 bytes Stores decimal numbers with double d = 3.14159;
double precision.
char 2 bytes (Unicode) Stores a single character or char c = 'A';
Unicode symbol.
Datatype Size Description Example
boolean 1 bit (JVM uses 1 byte) Stores true or false boolean flag = true;
2. Non-Primitive (Reference) Data
Types
• These are user-defined or complex types. They don’t store actual values directly, but references
(addresses) to the objects in memory.
• Examples:
• String → "Hello Java"
• Arrays → int[] arr = {1,2,3};
• Classes → Student s = new Student();
• Interfaces
variable
• A variable in Java is a named memory location that stores a value.
• It acts as a container for data that can be used and changed during
program execution.
Key Points about Variables:
• Declaration → A variable must be declared with a data type.
• int age; // declaring a variable of type int
• Initialization → Assigning a value to the variable.
• age = 20; // initializing
• Declaration + Initialization (common way):
• int age = 20;
public class Main {
public static void main(String[] args) {
int number = 10; // integer variable
double price = 99.99; // decimal variable
String name = "Manjula"; // string variable (reference type)
[Link](number);
[Link](price);
[Link](name);
}
}
Types of Variables in Java:
Local Variables – Declared inside methods, accessible only within that
method.
Instance Variables – Declared inside a class but outside methods,
belong to each object.
Static Variables (Class Variables) – Declared with static keyword, shared
by all objects of the class.
• Type Conversion and Casting in Java
• 1. Type Conversion (Type Casting between compatible types)
• Also called Type Casting, it’s when one data type is converted into
another.
• There are two types:
Implicit Type Conversion((Widening
Conversion / Type Promotion)
• Done automatically by Java.
• When a smaller type is converted into a larger type (no data loss).
• Order of promotion:
• byte → short → int → long → float → double
Example:
public class Main {
public static void main(String[] args) {
int num = 10;
double d = num; // int automatically converted to double
[Link](d); // Output: 10.0
}
}
• Explicit Type Conversion (Narrowing Conversion / Casting)
• Done manually by the programmer.
• Converts a larger type into a smaller type.
• Data loss may occur.
• Requires casting operator (type).
Example:
public class Main {
public static void main(String[] args) {
double d = 9.78;
int num = (int) d; // double manually cast to int
[Link](num); // Output: 9 (decimal part lost)
}
}
• 2. Type Casting with Objects (Upcasting & Downcasting)
• Upcasting → Assigning a child object to a parent reference (safe,
implicit).
• Downcasting → Assigning a parent reference to a child reference
(needs explicit cast)
class Animal { }
class Dog extends Animal {
void bark() { [Link]("Woof!"); }
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // Upcasting (implicit)
Dog d = (Dog) a; // Downcasting (explicit)
[Link](); // Output: Woof!
}
}
• Type Conversion (Widening) → Automatic, safe, no data loss.
• Type Casting (Narrowing) → Manual, possible data loss.
• Object Casting → Upcasting (safe) and Downcasting (needs explicit
cast