[Go to site: main page, start]

0% found this document useful (0 votes)
3 views20 pages

Java 2sem Notes

Uploaded by

girigiriyappa0
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)
3 views20 pages

Java 2sem Notes

Uploaded by

girigiriyappa0
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

Java Programing

Chapter 1st
OOP’s Concept:
1. Class
A class is a blueprint or template for creating objects.
It defines the properties (attributes) and behaviors (methods) of objects.
Example:
class Person {
String name;
int age;
}

2. Object
An object is an instance of a class.
It represents a real-world entity with state (attributes) and behavior (methods).
Example:
Person person = new Person(); // Creating an object of Person class

3. Encapsulation
Encapsulation means hiding the internal state of an object and only exposing
necessary details through methods.
Achieved by using private fields and public getter and setter methods.
Example:
class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}

4. Inheritance
The child class (subclass) inherits from the parent class (superclass) using the
extends keyword.
Example:
class Person {
String name;
}
class Student extends Person {
int studentId;
}

5. Polymorphism
Polymorphism means same name, different behavior.
It allows one interface to be used for different data types.
Achieved through method overloading and method overriding.
Example:
Method Overloading: Same method name, different parameters
class Display {
void show(int x) {
[Link](x);
}
void show(String y) {
[Link](y);
}
}
Method Overriding: Child class redefines parent class method
class Animal {
void sound() {
[Link]("Animal sound");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}

6. Abstraction
Abstraction is the process of hiding implementation details and showing only
essential features.
Achieved using abstract classes and interfaces.
Example:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
Data Types in Java:
1. Primitive Data Types
Primitive data types are the most basic data types in Java. They store simple
values directly in memory. Java has 8 primitive data types:
> byte, short, int, long, float, double, char, Boolean
Ex:
public class Main {
public static void main(String[] args) {
int a = 10;
float b = 5.5f;
double c = 10.123456789;
char d = 'A';
boolean e = true;

[Link]("int: " + a);


[Link]("float: " + b);
[Link]("double: " + c);
[Link]("char: " + d);
[Link]("boolean: " + e);
}
}
2. Non-Primitive Data Types
Non-primitive data types are more complex and can store multiple values or
objects. They are derived from the Object class.
➢ String, Arrays, Classes, Interfaces etc..
Ex:
public class Main {
public static void main(String[] args) {
// String
String name = "MAN";
[Link]("String: " + name);
// Array
int[] arr = {1, 2, 3};
[Link]("Array: " + arr[0]);
}
}
Operators:

1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations like addition,
subtraction, multiplication, division, and modulus.

Ex:
public class Main {
public static void main(String[] args) {
int a = 10, b = 3;
[Link]("Addition: " + (a + b)); // 13
[Link]("Subtraction: " + (a - b)); // 7
[Link]("Multiplication: " + (a * b)); // 30
[Link]("Division: " + (a / b)); // 3
[Link]("Modulus: " + (a % b)); // 1
}
}

2. Unary Operators
Unary operators work with a single operand.

Ex:
public class Main {
public static void main(String[] args) {
int a = 5;
[Link](+a); // 5
[Link](-a); // -5
[Link](++a); // 6 (pre-increment)
[Link](a++); // 6 (post-increment, then becomes 7)
[Link](--a); // 6 (pre-decrement)
[Link](a--); // 6 (post-decrement, then becomes 5)
boolean flag = true;
[Link] (!flag); // false
}
}

Assignment Operators
Assignment operators assign a value to a variable.
public class Main {
public static void main(String[] args) {
int a = 10;
a += 5; // a = a + 5 → 15
[Link](a);
a *= 2; // a = a * 2 → 30
[Link](a); } }

Relational Operators
Relational operators compare two values and return a boolean (true or false).

public class Main {


public static void main(String[] args) {
int a = 10, b = 5;
[Link](a == b); // false
[Link](a != b); // true
[Link](a > b); // true
[Link](a < b); // false
}
}

Logical Operators
Logical operators work with boolean values.
✓ Logical AND operator (&&)
✓ Logical OR operator (||)
✓ Logical NOT operator ( ! )

public class Main {


public static void main(String[] args) {
boolean a = true, b = false;
[Link](a && b); // false
[Link](a || b); // true
[Link](!a); // false
}
}
Ternary Operator
Ternary operator is a shorthand for if-else and it has 3 conditions.
Syntax: condition ? value_if_true : value_if_false;
public class Main {
public static void main (String[] args) {
int a = 5, b = 10;
int min = (a < b) ? a : b;
[Link] ("Minimum: " + min);
}
}

Bitwise Operators (Bit level operator)


Bitwise operators work at the bit-level.
✓ Bitwise OR (|)

Ex: public class Main {


public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
[Link](a | b); // 0111 → 7
}
}

✓ Bitwise AND (&)

Ex: public class Main {


public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
[Link](a & b); // 0001 → 1
}
}

✓ Bitwise XOR (^)


Returns 1 if the bits are different, otherwise returns 0.
Ex: public class Main {
public static void main(String[] args) {
int a = 5; // 0101
int b = 3; // 0011
[Link](a ^ b); // 0110 → 6
}
}
✓ Bitwise NOT ( ~ )
• Flips each bit (1 becomes 0, and 0 becomes 1).
• If the leftmost bit is 0, the number is positive.
• If the leftmost bit is 1, the number is negative – and the value is stored in two’s
complement form.

Ex: public class Main {


public static void main(String[] args) {
int a = 5; // 0101
[Link](~a); // -6
} }

Shift Operators (Bit shifting operator)

➢ Left shift:
• Shifts bits to the left by a specified number of positions.
• Each left shift multiplies the number by 2^n.

Ex: public class Main {


public static void main(String[] args) {
int a = 5; // 0101
[Link](a << 2); // 20
}
}

➢ Right shift:
• Shifts bits to the right by a specified number of positions.
• Each right shift divides the number by 2^n.
Ex: public class Main {
public static void main(String[] args) {
int a = 20; // 10100
[Link](a >> 2); // 5
}
}
➢ Unsigned right shift:
• Shifts bits to the right by a specified number of positions.
• Works differently for negative numbers.
• Example same as right shift.
Difference between Right shift and Unsigned Right shift

Control Structure in Java


1 Selection Statements
• If
Syntax:
if (condition) {
// Code to execute if condition is true
}
Ex :
public class Example {
public static void main(String[] args) {
int num = 10;
if (num > 5) {
[Link]("Number is greater than 5");
}
}
}
• if-else: Executes one block if the condition is true, otherwise executes the else block.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
EX: public class Example {
public static void main(String[] args) {
int num = 3;
if (num > 5) {
[Link]("Number is greater than 5");
} else {
[Link]("Number is less than or equal to 5");
}
}
}

• if-else-if: Used when there are multiple conditions to check.


Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Ex:
public class Example {
public static void main(String[] args) {
int num = 0;
if (num > 0) {
[Link]("Positive");
} else if (num < 0) {
[Link]("Negative");
} else {
[Link]("Zero");
}
}
}

• Switch: Used to execute one block of code based on the value of a variable.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
default:
// Code to execute if no cases match
}

Ex: public class Example {


public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
[Link]("Monday");
break;
case 2:
[Link]("Tuesday");
break;
case 3:
[Link]("Wednesday");
break;
default:
[Link]("Invalid day");
}
}
}

2. Looping: Looping statements execute a block of code repeatedly.


a) for loop:
Executes a block of code a fixed number of times.
Or
It will execute the block of code until the condition become false
Syntax:
for (initialization; condition; update) {
// Code to execute
}
ex:
public class Example {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
[Link](i);
}
}
}
b) while Loop:
A while loop is a type of loop in programming where the loop body executes only if
the specified condition is true. The condition is checked before the loop body
executes. If the condition is false at the beginning, the loop will not execute at all.

Syntax:
while (condition) {
/ / Code to execute
}
Ex:
public class Example {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link](i);
i++;
} } }

c) do while Loop:
A do...while loop is a type of loop in programming where the loop body is executed at least
once before the condition is checked. After executing the loop body, the condition is
evaluated; if the condition is true, the loop will repeat. If the condition is false, the loop will
terminate.

Syntax:
public class Example {
public static void main(String[] args) {
int i = 1;
do {
[Link](i);
i++;
} while (i <= 5);
} }

Ex:
public class Example {
public static void main(String[] args) {
int i = 1;
do {
[Link](i);
i++;
} while (i <= 5);
}
}

Difference Between while and Do while

Method Overloading:

Method overloading in Java allows you to define multiple methods with the same name but
with different parameter lists. It helps improve code readability and allows handling different
types of inputs using the same method name.
Example: // this program also example for defining Object and Class
class DisplayData {
void display (int num) {
[Link] ("Integer: " + num);
}
void display (int num, double value) {
[Link] ("Integer: " + num + ", Double: " + value);
}
void display(String text) {
[Link]("String: " + text);
}
}
public class Test {
public static void main(String[] args) {
DisplayData obj = new DisplayData();
[Link](10); // Calls display(int num)
[Link](5, 3.14); // Calls display(int num, double value)
[Link]("Java"); // Calls display(String text)
}
}

Math Class
The Math class in Java is a built-in class in the “[Link]” package that provides methods
and constants to perform mathematical operations like square root, power, trigonometry,
rounding, etc.
Some of the methods of Math class are given below with description:
Arrays

An array in Java is a data structure that stores a fixed-size collection of elements of the same
data type.
Syntax: dataType[] arrayName;
dataType[] arrayName = {value1, value2, value3, ...}; // Declaration, Initialization, and
Assignment

Two types of Array:


❖ Single-Dimensional Array – A list of elements stored in a single row or column.

Ex: public class Test {


public static void main(String[] args) {
// Declaration and Initialization
int[] arr = {10, 20, 30, 40, 50};

for (int i = 0; i < [Link]; i++) {


[Link]("Element at index " + i + ": " + arr[i]);
}}}

❖ Multi-Dimensional Array – An element stored in a rows and columns. (matrix)

Ex: public class Test {


public static void main(String[] args) {
// Declaration and Initialization of 2D Array
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Accessing elements using nested loops


for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
}
Constructors:

A constructor in Java is a special method that is called automatically when an object of


a class is created. It is used to initialize the object with default or user-defined values.
Points:

The name of the constructor must be the same as the class name.
A constructor does not have a return type (not even void).
It is called automatically when an object is created.
You can have multiple constructors in a class (constructor overloading).

Types of constructors:
➢ Default Constructor
➢ Parameterized Constructor
➢ Copy Constructor
1. Default Constructor: A default constructor is automatically created when object is created
Ex :
class Car {
Car() {
[Link] ("Car object created!");
}
}
public class Test {
public static void main(String[] args) {
Car car1 = new Car();
}
}
2. Parameterized Constructor:
A parameterized constructor in Java is a constructor that takes arguments (parameters) to
initialize the object with specific values when it is created.
Ex:
class Car {
String brand;
int speed;

// Parameterized Constructor
Car(String b, int s) {
brand = b;
speed = s;
}
void display() {
[Link]("Brand: " + brand);
[Link]("Speed: " + speed);
}
}

public class Test {


public static void main(String[] args) {
// Creating an object with parameters
Car car1 = new Car("Toyota", 120);
[Link]();
}
}
[Link] Constructor:

A copy constructor in Java is a constructor that creates a new object by copying the values
of an existing object.
Example:
class Car {
String brand;
int speed;
Car(String b, int s) { // Parameterized Constructor
brand = b;
speed = s;
}
Car(Car c) { // Copy Constructor
brand = [Link];
speed = [Link];
}
void display() {
[Link]("Brand: " + brand);
[Link]("Speed: " + speed);
}
}
public class Test {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 120); // Original object
Car car2 = new Car(car1); // Copy object using copy constructor
[Link](); // Display copied values
}
}

Visibility modifiers

1. private
• Accessible only within the same class.
• Not accessible from other classes, even if they are in the same package.
• Example: encapsulation.
2. protected
• Accessible within the same package and in subclasses (even in different packages).
• Used when you want to allow access in subclasses but not in other unrelated classes.
• Example: Inheritance
3. public
• Accessible from anywhere (within the same package or from other packages).
• Used when you want to make the class or method completely accessible.

You might also like