Module 1 Java
Module 1 Java
● Variable Declaration
● Java Tokens
● Arrays
● Operators in Java
○ Operators Introduction
○ Operator Precedence
● Control Statements
● Introduction to classes
● Instance Variables
● Class Variables
● Instance Methods
● Constructors
● Declaring Object
● Garbage Collection
● Method Overloading
● Constructor Overloading
● this reference
● Recursion
● Access Modifiers
● Inner Class
Java was developed by James Gosling and his team at Sun Microsystems in the early 1990s.
Originally named Oak, it was designed for embedded systems in consumer electronics. Later, it
was renamed to Java, inspired by Java coffee.
Key milestones:
● Simple: Easy syntax, similar to C++ but without complex features like pointers.
4. Java Architecture
Flow:
Source Code (.java) → javac → Bytecode (.class) → java → Output
● jdb – Debugger
Execution is the process where the compiled bytecode (.class file) is run by the Java Virtual
Machine (JVM) to produce output.
What is an Array?
● An array is a collection of elements of the same data type stored in a contiguous
block of memory.
● Arrays have a fixed size — you must specify the size when creating the array.
Initializing Arrays
You can initialize an array at the time of declaration:
System.out.println(numbers[i]);
Important Points
● Arrays in Java are objects.
● Array size is fixed once created; to change size, create a new array.
● Arrays can hold primitive types (int, char, etc.) or objects (like String).
A variable is a named memory location that stores data which can be changed during program
execution.
○ Cannot be a keyword
○ Case sensitive
Variable Initialization:
● They store references (addresses) to memory where the actual data is located.
Type Casting is the process of converting a variable from one data type to another. It’s useful
when you need to work with different types together or store data in a different format.
Example:
int i = 100;
double d = i; // int to double automatically
System.out.println(d); // Output: 100.0
2. Narrowing Casting (Explicit Casting)
Example:
double d = 100.04;
int i = (int) d; // Explicit cast from double to int
● Assign values
● Manipulate bits
Important Points:
● Operators with higher precedence are evaluated before those with lower precedence.
● Operators with the same precedence are evaluated based on associativity (usually left
to right).
● You can always use parentheses () to override the default precedence to ensure the
desired order of evaluation.
Logical OR `
1. Conditional Statements
if statement
if-else statement
switch statement
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}
2. Looping Statements
for loop
Used when number of iterations is known.
while loop
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
do-while loop
Executes the block at least once, then repeats while condition is true.
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
3. Jump Statements
● continue: Skips the current iteration and jumps to the next iteration of the loop.
● return: Exits from the current method and optionally returns a value.
Classes in Java
What is a Class?
● It defines attributes (variables) and behaviors (methods) that the objects created from
the class will have.
● It encapsulates data for the object and methods to manipulate that data.
// Method (behavior)
void sayHello() {
System.out.println("Hello, my name is " + name);
}
}
What is an Object?
● An object is an instance of a class.
● Objects have state (stored in instance variables) and behavior (defined by methods).
● You use objects to access the properties and behaviors defined in the class.
void sayHello() {
System.out.println("Hello, my name is " + name);
}
}
Creating an Object
● You can create multiple objects from the same class with different states.
Definition:
Instance variables are non-static fields defined in a class and are associated with each object
created from that class. Every object has its own copy of these variables. They represent the
state or properties of an object.
Features:
Real-life Analogy:
Think of a Student class. Each student (object) will have a different name and roll number
(instance variables).
Example:
class Student {
int rollNo;
String name;
void display() {
System.out.println("Roll No: " + rollNo + ", Name: " + name);
}
}
Definition:
Static variables belong to the class, not to instances of the class. They are shared across all
instances and are initialized only once.
Features:
● Declared using the static keyword.
Real-life Analogy:
If all students belong to the same college, we can declare college as a static variable.
Example:
class Student {
int rollNo;
String name;
static String college = "BBDU";
void display() {
System.out.println(rollNo + " " + name + " " + college);
}
}
Instance Methods
Definition:
Instance methods are functions that operate on instance variables. These methods require an
object to be called and can modify the object's state.
Features:
Example:
class Student {
int marks;
void setMarks(int m) {
this.marks = m;
}
void getMarks() {
System.out.println("Marks: " + marks);
}
}
Constructors
Definition:
A constructor is a special block of code called automatically when an object is created. It is
used to initialize instance variables.
Features:
● No return type.
Types:
● Parameterized constructor.
Example:
class Student {
int id;
String name;
Student(int i, String n) {
id = i;
name = n;
}
void display() {
System.out.println(id + " " + name);
}
}
Declaring Object
Definition:
Creating an object involves allocating memory for the class instance using the new keyword.
The constructor initializes the object.
Syntax:
Example:
Real-life Analogy:
Declaring an object is like creating a new account with specific details (name, ID).
Garbage Collection
Definition:
Garbage Collection is a process by which Java automatically removes unused objects from
memory to prevent memory leaks and optimize performance.
How it works:
Example:
Student s = new Student();
s = null; // Object becomes unreachable
System.gc(); // Requests garbage collection
Method Overloading
Definition:
Method Overloading allows defining multiple methods with the same name but different
parameter lists within the same class.
Purpose:
● Improves readability.
● Supports polymorphism.
Example:
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}
Constructor Overloading
Definition:
Constructor Overloading is the practice of defining multiple constructors in the same class,
each with a different set of parameters.
Use Case:
Allows object initialization in different ways.
Example:
class Student {
int id;
String name;
Student() {
id = 0;
name = "Unknown";
}
Student(int i, String n) {
id = i;
name = n;
}
}
9. this Reference
Definition:
The this keyword is a reference variable that refers to the current object inside a class. It's
used to resolve naming conflicts between instance variables and parameters.
Uses:
Example:
class Student {
int id;
Student(int id) {
this.id = id; // 'this' refers to the current object
}
}
Using Objects in Methods
Definition:
Objects can be passed as parameters to methods or returned from methods. This enables
object interaction and modular design.
Example:
class Student {
int id;
void show(Student s) {
System.out.println("ID: " + s.id);
}
}
Usage:
● Comparing objects.
11. Recursion
Definition:
Recursion is the process where a method calls itself repeatedly to solve a smaller
subproblem. It must have a base condition to terminate.
Use Cases:
● Factorial calculation
● Fibonacci series
● Tree traversals
Example:
int factorial(int n) {
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}
Definition:
Access Modifiers control the visibility and accessibility of classes, methods, and variables.
Types:
private Yes No No No
Example:
Definition:
An inner class is a class defined within another class. It helps logically group classes that are
only used in one place, and enhances encapsulation.
Types:
● Non-static inner class: Access outer class members directly.
● Anonymous inner class: Class without a name used for short-term use.
Example:
class Outer {
int x = 10;
class Inner {
void display() {
System.out.println("x = " + x);
}
}
}