UNIT 1 JAVA
UNIT 1 JAVA
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
1. Introduc on to Java
What is Java?
Simple: Java is easy to understand because it removes complex things from older
languages like C++.
Pla orm-Independent: Write Java code once, and it runs on any device with a Java
Virtual Machine (JVM).
Mul threaded: Java can do many tasks at once (like playing music and showing a
video).
3. JVM Concepts
What is JVM?
o JVM (Java Virtual Machine) is a program that runs Java code on any device.
o Example: You write a Java app on your laptop, and JVM lets it run on your
phone.
Example:
java
Copy
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).
How to Run:
1. Save as Hello.java.
Variables:
o Example:
java
Copy
java
Copy
6. Java Keywords
o Keywords are special words in Java that have a fixed meaning. You can’t use
them as variable names.
Examples of Keywords:
Example:
java
Copy
1. Data Types
o Data types tell Java what kind of data you’re working with (numbers, text,
etc.).
These are more complex, like String (for text), arrays, or classes.
Declaring:
Ini alizing:
java
Copy
Example:
java
Copy
System.out.println("Age: " + age + ", Salary: " + salary + ", City: " + city);
3. Java Operators
Types of Operators:
o Arithme c Operators:
o Comparison Operators:
o Logical Operators:
Example: if (age > 18 && marks > 50) (both must be true).
Example:
java
Copy
int a = 10, b = 5;
int sum = a + b; // 15
int diff = a - b; // 5
System.out.println("Sum: " + sum + ", Diff: " + diff + ", Equal: " + isEqual);
What is a Class?
What is an Object?
o An object is something you create from a class—it’s like the real thing made
from the blueprint.
Example:
java
Copy
void drive() {
System.out.println("Car is driving!");
myCar.drive();
o Output:
text
Copy
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 No return type.
Example:
java
Copy
String color;
Car(String c) { // Constructor
color = c;
}
3. Overloading Constructors
What is Overloading?
o Example: One constructor sets only the color, another sets color and speed.
Example:
java
Copy
String color;
int speed;
Car(String c) { // Constructor 1
color = c;
color = c;
speed = s;
o Output:
text
Copy
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
String color;
Car(String color) {
Example:
java
Copy
String color;
Car(String color) {
this.color = color;
void checkColor(Car c) {
myCar.checkColor(myCar);
6. Argument Passing
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 {
num = 100;
void changeCarColor(Car c) {
c.color = "Black";
obj.changeNumber(number);
obj.changeCarColor(myCar);
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?
Example:
java
Copy
String color;
Car(String color) {
this.color = color;
Car createCar() {
8. Method Overloading
o It’s having mul ple methods with the same name but different parameters.
Example:
java
Copy
return a + b;
return a + b + c;
o Output:
text
Copy
Sum of 2 numbers: 8
Sum of 3 numbers: 10
9. 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
class Car {
String color;
Car(String color) {
this.color = color;
final:
o Example:
finalize:
o Example:
java
Copy
obj = null;
System.gc();
finally:
o Example:
java
Copy
try {
int x = 10 / 0;
} catch (Excep on e) {
System.out.println("Error!");
} finally {
}
o Output:
text
Copy
Error!
Java:
o No pointers (safer).
C++:
12. Inheritance
What is Inheritance?
o It’s when a class (child) gets features (methods, variables) from another class
(parent).
Example:
java
Copy
class Car {
System.out.println("Driving!");
void speedUp() {
System.out.println("Speeding up!");
myCar.drive();
myCar.speedUp();
o Output:
text
Copy
Color: Red
Driving!
Speeding up!
13. Interface
What is an Interface?
java
Copy
interface Drawable {
void draw();
System.out.println("Drawing a circle!");
c.draw();
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
System.out.println("This is a shape.");
void draw() {
System.out.println("Drawing a rectangle!");
r.draw();
r.info();
o Output:
text
Copy
Drawing a rectangle!
This is a shape.
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();
System.out.println("Duck is flying!");
System.out.println("Duck is swimming!");
d.fly();
d.swim();
o Output:
text
Copy
Duck is flying!
Duck is swimming!
Summary for Your Exam
Principles of OOP:
o Primi ve types (int, double), variables, and keywords (class, if) are basics.
Data Types:
o Declare and ini alize variables, use operators (+, ==, &&).
o Overload methods and constructors, use this, pass objects, return objects.
o Garbage collec on cleans memory, final locks values, finalize runs before
dele on, finally is for cleanup.