0% found this document useful (0 votes)
2 views

UNIT 1 JAVA

This document provides an introduction to Object-Oriented Programming (OOP) principles using Java, covering basic features, data types, and Java Virtual Machine (JVM) concepts. It explains key concepts such as classes, objects, methods, constructors, method overloading, and garbage collection, along with examples of Java code. Additionally, it highlights the differences between Java and C++, and discusses the significance of keywords and operators in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UNIT 1 JAVA

This document provides an introduction to Object-Oriented Programming (OOP) principles using Java, covering basic features, data types, and Java Virtual Machine (JVM) concepts. It explains key concepts such as classes, objects, methods, constructors, method overloading, and garbage collection, along with examples of Java code. Additionally, it highlights the differences between Java and C++, and discusses the significance of keywords and operators in Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT-1

Principles of Object- Oriented Programming

Introduc on to Java, Basic Features, JVM Concepts, A Simple Java Program, Primi ve Data
Type and Variables, Java Keywords

Data Types

Data Types, Declaring and Ini aliza on Variables, Java Operators. Class, Object &
Methods, Constructors, Overloading constructors, This Keyword, Using Objects as
Parameters, Argument passing, returning objects,

Method Overloading

Method Overloading, Garbage Collec on, Final, Finalize and Finally Method, Comparison
between Java and C++. Inheritance, Interface, Abstract Class,Interfaces

TOPICS TO BE COVERED

Principles of Object-Oriented Programming (OOP)

1. Introduc on to Java

 What is Java?

o Java is a popular programming language used to build apps, websites, and


games. It’s simple, secure, and works on any device (like computers, phones).

o Example: Apps like Minecra are made with Java.

 Why Use Java?

o It’s easy to learn.

o It works everywhere (Windows, Mac, Linux).

o It’s safe for building web apps.

2. Basic Features of Java

 Simple: Java is easy to understand because it removes complex things from older
languages like C++.

o Example: No need to worry about pointers (a tricky concept in C++).


 Object-Oriented: Java uses OOP (explained below) to organize code like real-world
objects.

 Pla orm-Independent: Write Java code once, and it runs on any device with a Java
Virtual Machine (JVM).

o Example: A Java game you write on Windows will work on a Mac.

 Secure: Java has safety features to stop viruses or hacks.

 Portable: You can move Java code easily between devices.

 Mul threaded: Java can do many tasks at once (like playing music and showing a
video).

 High Performance: Java is fast because of the JVM.

3. JVM Concepts

 What is JVM?

o JVM (Java Virtual Machine) is a program that runs Java code on any device.

o Example: It’s like a translator—your Java code is translated by JVM so your


computer understands it.

 How JVM Works:

1. You write Java code (e.g., Hello.java).

2. You compile it into bytecode using javac (creates Hello.class).

3. JVM reads the bytecode and runs it on your device.

 Why is JVM Important?

o It makes Java work on any device (Windows, Mac, etc.).

o Example: You write a Java app on your laptop, and JVM lets it run on your
phone.

4. A Simple Java Program

 Example:

java

Copy

public class Hello {

public sta c void main(String[] args) {


System.out.println("Hello, Java!");

 Explana on:

o public class Hello: Creates a class named Hello (a class is like a blueprint).

o public sta c void main(String[] args): The star ng point of your program (Java
runs this first).

o System.out.println("Hello, Java!");: Prints “Hello, Java!” on the screen.

 How to Run:

1. Save as Hello.java.

2. Compile: javac Hello.java (creates Hello.class).

3. Run: java Hello.

o Output: Hello, Java!

5. Primi ve Data Types and Variables

 What Are Primi ve Data Types?

o These are basic types of data in Java, like numbers or le ers.

 Common Primi ve Data Types:

o int: For whole numbers (e.g., 10, -5).

o double: For decimal numbers (e.g., 3.14).

o char: For single le ers or symbols (e.g., 'A', '@').

o boolean: For true/false values.

 Variables:

o A variable is like a box where you store data.

o Example:

java

Copy

int age = 20; // Stores the number 20

double price = 99.99; // Stores a decimal


char grade = 'A'; // Stores a le er

boolean isStudent = true; // Stores true/false

 How to Use Variables:

java

Copy

public class VariableExample {

public sta c void main(String[] args) {

int score = 95;

System.out.println("Your score is: " + score);

o Output: Your score is: 95

6. Java Keywords

 What Are Keywords?

o Keywords are special words in Java that have a fixed meaning. You can’t use
them as variable names.

 Examples of Keywords:

o class: To define a class.

o public: To make something accessible.

o int: To define a number variable.

o if: For condi ons.

o return: To return a value.

 Example:

o You can’t name a variable class because it’s a keyword:

java

Copy

int class = 10; // Wrong! 'class' is a keyword

int myClass = 10; // Correct


Data Types in Java

1. Data Types

 What Are Data Types?

o Data types tell Java what kind of data you’re working with (numbers, text,
etc.).

 Two Main Types:

o Primi ve Data Types (already covered):

 byte, short, int, long (for numbers).

 float, double (for decimals).

 char (for le ers).

 boolean (for true/false).

o Non-Primi ve Data Types:

 These are more complex, like String (for text), arrays, or classes.

 Example: String name = "Amit"; (stores a name).

2. Declaring and Ini alizing Variables

 Declaring:

o This means telling Java the type and name of a variable.

o Example: int marks; (declares a variable for numbers).

 Ini alizing:

o This means giving the variable a value.

o Example: marks = 85; (sets the value to 85).

 Declaring and Ini alizing Together:

java

Copy

int marks = 85;

String name = "Amit";

 Example:
java

Copy

public class DataTypeExample {

public sta c void main(String[] args) {

int age = 20;

double salary = 5000.50;

String city = "Delhi";

System.out.println("Age: " + age + ", Salary: " + salary + ", City: " + city);

o Output: Age: 20, Salary: 5000.5, City: Delhi

3. Java Operators

 What Are Operators?

o Operators are symbols that do things like math, comparison, or logic.

 Types of Operators:

o Arithme c Operators:

 + (add), - (subtract), * (mul ply), / (divide), % (remainder).

 Example: int sum = 10 + 5; (sum is 15).

o Comparison Operators:

 == (equal), != (not equal), <, >, <=, >=.

 Example: if (age > 18) (checks if age is more than 18).

o Logical Operators:

 && (and), || (or), ! (not).

 Example: if (age > 18 && marks > 50) (both must be true).

 Example:

java

Copy

public class OperatorExample {


public sta c void main(String[] args) {

int a = 10, b = 5;

int sum = a + b; // 15

int diff = a - b; // 5

boolean isEqual = (a == b); // false

System.out.println("Sum: " + sum + ", Diff: " + diff + ", Equal: " + isEqual);

o Output: Sum: 15, Diff: 5, Equal: false

Method Overloading and Related Concepts

1. Class, Object & Methods

 What is a Class?

o A class is like a blueprint for crea ng objects.

o Example: A Car class is a blueprint for making car objects.

 What is an Object?

o An object is something you create from a class—it’s like the real thing made
from the blueprint.

o Example: If Car is the class, myCar is an object (a specific car).

 What are Methods?

o Methods are ac ons an object can do.

o Example: A Car can drive()—that’s a method.

 Example:

java

Copy

public class Car {

String color = "Red";

void drive() {
System.out.println("Car is driving!");

public sta c void main(String[] args) {

Car myCar = new Car(); // Create object

System.out.println("Car color: " + myCar.color);

myCar.drive();

o Output:

text

Copy

Car color: Red

Car is driving!

2. Constructors

 What is a Constructor?

o A constructor is a special method that runs when you create an object. It sets
up the object.

o Example: When you make a Car object, the constructor can set its color.

 Rules:

o Same name as the class.

o No return type.

 Example:

java

Copy

public class Car {

String color;

Car(String c) { // Constructor

color = c;
}

public sta c void main(String[] args) {

Car myCar = new Car("Blue");

System.out.println("Car color: " + myCar.color);

o Output: Car color: Blue

3. Overloading Constructors

 What is Overloading?

o Overloading means having mul ple constructors with different inputs.

o Example: One constructor sets only the color, another sets color and speed.

 Example:

java

Copy

public class Car {

String color;

int speed;

Car(String c) { // Constructor 1

color = c;

Car(String c, int s) { // Constructor 2

color = c;

speed = s;

public sta c void main(String[] args) {

Car car1 = new Car("Red");

Car car2 = new Car("Blue", 60);

System.out.println("Car 1: " + car1.color + ", Speed: " + car1.speed);


System.out.println("Car 2: " + car2.color + ", Speed: " + car2.speed);

o Output:

text

Copy

Car 1: Red, Speed: 0

Car 2: Blue, Speed: 60

4. this Keyword

 What is this?

o this refers to the current object. It’s used to avoid confusion between
variables.

o Example: If a class has a variable color and a method uses a parameter color,
this.color means the class variable.

 Example:

java

Copy

public class Car {

String color;

Car(String color) {

this.color = color; // 'this.color' is the class variable

public sta c void main(String[] args) {

Car myCar = new Car("Green");

System.out.println("Car color: " + myCar.color);

o Output: Car color: Green

5. Using Objects as Parameters


 What is This?

o You can pass an object to a method as a parameter.

o Example: Pass a Car object to a method to check its color.

 Example:

java

Copy

public class Car {

String color;

Car(String color) {

this.color = color;

void checkColor(Car c) {

System.out.println("This car is: " + c.color);

public sta c void main(String[] args) {

Car myCar = new Car("Yellow");

myCar.checkColor(myCar);

o Output: This car is: Yellow

6. Argument Passing

 What is Argument Passing?

o It’s how you send values to a method.

o Java uses pass-by-value for primi ves (like int) and pass-by-reference for
objects (but the reference is copied).

 Example:

java

Copy
public class ArgumentExample {

void changeNumber(int num) {

num = 100;

void changeCarColor(Car c) {

c.color = "Black";

public sta c void main(String[] args) {

int number = 10;

ArgumentExample obj = new ArgumentExample();

obj.changeNumber(number);

System.out.println("Number: " + number); // S ll 10 (pass-by-value)

Car myCar = new Car("White");

obj.changeCarColor(myCar);

System.out.println("Car color: " + myCar.color); // Black (pass-by-reference)

class Car {

String color;

Car(String color) {

this.color = color;

o Output:

text

Copy

Number: 10
Car color: Black

7. Returning Objects

 What is This?

o A method can return an object.

o Example: A method creates a Car object and returns it.

 Example:

java

Copy

public class Car {

String color;

Car(String color) {

this.color = color;

Car createCar() {

return new Car("Purple");

public sta c void main(String[] args) {

Car factory = new Car("Red");

Car newCar = factory.createCar();

System.out.println("New car color: " + newCar.color);

o Output: New car color: Purple

8. Method Overloading

 What is Method Overloading?

o It’s having mul ple methods with the same name but different parameters.

o Example: A method add can add two numbers or three numbers.

 Example:
java

Copy

public class OverloadExample {

int add(int a, int b) {

return a + b;

int add(int a, int b, int c) {

return a + b + c;

public sta c void main(String[] args) {

OverloadExample calc = new OverloadExample();

System.out.println("Sum of 2 numbers: " + calc.add(5, 3));

System.out.println("Sum of 3 numbers: " + calc.add(5, 3, 2));

o Output:

text

Copy

Sum of 2 numbers: 8

Sum of 3 numbers: 10

9. Garbage Collec on

 What is Garbage Collec on?

o Java automa cally cleans up memory by removing objects you don’t need.

o Example: If you create a Car object and stop using it, Java deletes it to free
memory.

 How It Works:

o The Garbage Collector (GC) runs in the background and finds unused objects.

o You can suggest GC to run using System.gc(), but it’s not guaranteed.
 Example:

java

Copy

public class GarbageCollec onExample {

public sta c void main(String[] args) {

Car myCar = new Car("Red");

myCar = null; // Object is now unused

System.gc(); // Suggest garbage collec on

System.out.println("Garbage collec on suggested!");

class Car {

String color;

Car(String color) {

this.color = color;

10. final, finalize, and finally

 final:

o A keyword to make something unchangeable.

o Example:

 final int x = 10; (can’t change x).

 final class Car (can’t inherit from Car).

 final void drive() (can’t override the method).

 finalize:

o A method called before an object is deleted by garbage collec on.

o Example:

java
Copy

public class FinalizeExample {

protected void finalize() {

System.out.println("Object is being deleted!");

public sta c void main(String[] args) {

FinalizeExample obj = new FinalizeExample();

obj = null;

System.gc();

o Output: Object is being deleted!

 finally:

o A block that always runs a er a try-catch, used for cleanup.

o Example:

java

Copy

public class FinallyExample {

public sta c void main(String[] args) {

try {

int x = 10 / 0;

} catch (Excep on e) {

System.out.println("Error!");

} finally {

System.out.println("This always runs!");

}
o Output:

text

Copy

Error!

This always runs!

11. Comparison Between Java and C++

 Java:

o Pla orm-independent (runs on JVM).

o No pointers (safer).

o Automa c garbage collec on.

o No mul ple inheritance (uses interfaces).

o Example: String class is built-in.

 C++:

o Pla orm-dependent (needs compiling for each OS).

o Uses pointers (can be risky).

o Manual memory management.

o Supports mul ple inheritance.

o Example: No built-in String (uses char arrays).

12. Inheritance

 What is Inheritance?

o It’s when a class (child) gets features (methods, variables) from another class
(parent).

o Example: A SportsCar class can inherit from a Car class.

 Example:

java

Copy

class Car {

String color = "Red";


void drive() {

System.out.println("Driving!");

class SportsCar extends Car {

void speedUp() {

System.out.println("Speeding up!");

public class InheritanceExample {

public sta c void main(String[] args) {

SportsCar myCar = new SportsCar();

System.out.println("Color: " + myCar.color);

myCar.drive();

myCar.speedUp();

o Output:

text

Copy

Color: Red

Driving!

Speeding up!

13. Interface

 What is an Interface?

o An interface is a contract that a class must follow—it lists methods but


doesn’t define them.

o Example: An interface Drawable says “you must have a draw() method.”


 Example:

java

Copy

interface Drawable {

void draw();

class Circle implements Drawable {

public void draw() {

System.out.println("Drawing a circle!");

public class InterfaceExample {

public sta c void main(String[] args) {

Circle c = new Circle();

c.draw();

o Output: Drawing a circle!

14. Abstract Class

 What is an Abstract Class?

o An abstract class is a class that can’t be used to make objects directly. It’s a
par al blueprint.

o Example: An abstract Shape class has a method draw(), but each shape
defines it differently.

 Example:

java

Copy

abstract class Shape {

abstract void draw();


void info() {

System.out.println("This is a shape.");

class Rectangle extends Shape {

void draw() {

System.out.println("Drawing a rectangle!");

public class AbstractClassExample {

public sta c void main(String[] args) {

Rectangle r = new Rectangle();

r.draw();

r.info();

o Output:

text

Copy

Drawing a rectangle!

This is a shape.

15. Interfaces (More Details)

 Why Use Interfaces?

o To allow mul ple classes to share the same methods.

o Java doesn’t allow mul ple inheritance with classes, but you can implement
mul ple interfaces.

 Example:

java
Copy

interface Flyable {

void fly();

interface Swimmable {

void swim();

class Duck implements Flyable, Swimmable {

public void fly() {

System.out.println("Duck is flying!");

public void swim() {

System.out.println("Duck is swimming!");

public class Mul pleInterfaces {

public sta c void main(String[] args) {

Duck d = new Duck();

d.fly();

d.swim();

o Output:

text

Copy

Duck is flying!

Duck is swimming!
Summary for Your Exam

 Principles of OOP:

o Java is simple, secure, and pla orm-independent (thanks to JVM).

o Primi ve types (int, double), variables, and keywords (class, if) are basics.

 Data Types:

o Primi ve (int, char) and non-primi ve (String).

o Declare and ini alize variables, use operators (+, ==, &&).

 Method Overloading and More:

o Classes create objects, methods define ac ons, constructors set up objects.

o Overload methods and constructors, use this, pass objects, return objects.

o Inheritance, interfaces, and abstract classes help organize code.

o Garbage collec on cleans memory, final locks values, finalize runs before
dele on, finally is for cleanup.

You might also like