0% found this document useful (0 votes)
2 views10 pages

oops in java

This document covers the fundamentals of Java programming, focusing on Object Oriented Programming (OOP) concepts such as classes, objects, inheritance, polymorphism, and encapsulation. It also outlines key Java features, the Java Virtual Machine (JVM), data types, type casting, operators, and expressions. Additionally, it provides examples of Java programs and command line arguments.

Uploaded by

bittuvema379
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 views10 pages

oops in java

This document covers the fundamentals of Java programming, focusing on Object Oriented Programming (OOP) concepts such as classes, objects, inheritance, polymorphism, and encapsulation. It also outlines key Java features, the Java Virtual Machine (JVM), data types, type casting, operators, and expressions. Additionally, it provides examples of Java programs and command line arguments.

Uploaded by

bittuvema379
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

UNIT 1: Java Programming – Fundamentals

1. Basic Concepts of Object Oriented Programming (OOP)

Object Oriented Programming organizes so ware as a collec on of objects that contain both data
and behavior. Java is a pure OOP language (almost).

1.1 Objects and Classes

 Class: Blueprint or template from which objects are created. It describes the data
(a ributes) and methods (behavior) common to all objects of that kind.

 Object: Instance of a class. It has state (values of a ributes) and behavior (methods).

Example:

java

class Car {

String color;

void start() { [Link]("Car started"); }

Car myCar = new Car(); // myCar is an object

1.2 Data Abstrac on & Encapsula on

 Abstrac on: Hiding implementa on details and showing only essen al features. Achieved
using abstract classes and interfaces.

 Encapsula on: Wrapping data and methods into a single unit (class) and hiding internal
data using private access modifiers. Exposed via public ge er/se er methods.

Example:

java

class BankAccount {

private double balance; // hidden

public void deposit(double amt) { balance += amt; }

public double getBalance() { return balance; }

1.3 Inheritance

 Mechanism where one class inherits fields and methods from another class.

 Parent class (superclass) and Child class (subclass).

 Keyword: extends

 Promotes code reuse and method overriding.


Example:

java

class Vehicle { void move() { [Link]("Moving"); } }

class Bike extends Vehicle { void move() { [Link]("Bike moving"); } }

1.4 Polymorphism

 Ability of an object to take many forms.

 Two types:

o Compile- me (method overloading): mul ple methods with same name but
different parameters.

o Run me (method overriding): subclass provides specific implementa on of parent


method.

1.5 Dynamic Binding

 Also called late binding. The method to be executed is determined at run me based on the
actual object type, not the reference type.

 Achieved through method overriding.

1.6 Message Passing

 Objects communicate by sending messages (method calls) to each other.

 Example: [Link](parameters)

2. Java Features

Java is designed with following key features (o en called “Java buzzwords”):

1. Simple: No pointers, operator overloading (except limited), automa c memory


management.

2. Object Oriented: Everything (except primi ves) is an object.

3. Pla orm Independent: Bytecode runs on any JVM.

4. Secure: No explicit pointer, bytecode verifica on, run me security manager.

5. Robust: Strong memory management, excep on handling, type checking.

6. Architecture Neutral: Not ed to a specific machine.

7. Portable: Same results regardless of hardware.

8. Mul threaded: Built-in thread support.

9. Interpreted & High Performance: JIT compila on speeds execu on.

10. Dynamic: Can load classes at run me.


3. Java Virtual Machine (JVM) & Bytecode Interpreta on

3.1 JVM

 JVM is an abstract machine that executes Java bytecode.

 It provides run me environment: memory alloca on, garbage collec on, security.

 JVMs exist for every pla orm (Windows, Linux, Mac, etc.).

3.2 Bytecode Interpreta on

 Java compiler (javac) converts .java source files into .class files containing bytecode.

 Bytecode is pla orm independent – not machine code.

 JVM interprets (or JIT-compiles) bytecode into na ve machine code at run me.

Flow:
.java → javac → .class (bytecode) → JVM → Machine code

Advantage: Write once, run anywhere (WORA).

4. Simple Java Program & Command Line Arguments

4.1 First Java Program

java

// [Link]

public class HelloWorld {

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

[Link]("Hello, Java!");

Explana on:

 public class HelloWorld – class name must match filename.

 public sta c void main(String[] args) – entry point.

o public – accessible to JVM.

o sta c – called without object.

o void – returns nothing.

o String[] args – command line arguments.

 [Link] – prints to console.


Compile & Run:

bash

javac [Link]

java HelloWorld

4.2 Command Line Arguments

 Passed to main as String[] args.

 Example:

java

public class CmdArgs {

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

[Link]("First arg: " + args[0]);

[Link]("Number of args: " + [Link]);

Run: java CmdArgs Hello 123


Output:

text

First arg: Hello

Number of args: 2

5. Data Types in Java

Java has two categories of data types:

5.1 Primi ve Data Types

Type Size Range / Example

byte 1 byte -128 to 127

short 2 bytes -32,768 to 32,767

int 4 bytes -2³¹ to 2³¹-1

long 8 bytes -2⁶³ to 2⁶³-1 (suffix L)


Type Size Range / Example

float 4 bytes ~ ±3.4e38 (suffix f)

double 8 bytes ~ ±1.8e308

char 2 bytes Unicode (0 to 65,535)

boolean 1 bit (conceptually) true / false

Example:

java

int age = 25;

double pi = 3.14159;

char grade = 'A';

boolean isJavaFun = true;

5.2 Reference Types

 Arrays, classes, interfaces, enums, strings.

 Default value is null.

6. Type Cas ng (Type Conversion)

Conver ng one data type to another.

6.1 Implicit (Widening) – Automa c

 Conver ng smaller type to larger type (no data loss).

 byte → short → int → long → float → double

java

int x = 100;

long y = x; // automa c

float z = y; // automa c

6.2 Explicit (Narrowing) – Manual

 Conver ng larger type to smaller type – requires cast operator (type).

 Possible data loss.

java
double d = 9.78;

int i = (int) d; // i = 9 (frac on lost)

6.3 Type Promo on in Expressions

 byte/short/char are promoted to int during expression evalua on.

 Example: byte b = 10; b = b * 2; → error. Fix: b = (byte)(b * 2);

7. Operators in Java

7.1 Arithme c Operators

Operator Meaning Example

+ addi on 5+2=7

- subtrac on 5-2=3

* mul plica on 5 * 2 = 10

/ division 5 / 2 = 2 (int), 5.0/2 = 2.5

% modulus (remainder) 5%2=1

7.2 Increment / Decrement Operators

 ++ : increment by 1

 -- : decrement by 1

 Prefix (++a) : change then use

 Pos ix (a++) : use then change

java

int a = 5;

int b = ++a; // a=6, b=6

int c = a++; // a=7, c=6

7.3 Rela onal Operators

Return boolean (true/false)


Operator Meaning

== equal to

!= not equal

> greater than

< less than

>= greater or equal

<= less or equal

7.4 Logical Operators

Work with boolean operands.

Operator Descrip on

&& logical AND (short-circuit)

|| logical OR (short-circuit)

! logical NOT

java

boolean res = (5 > 3) && (2 < 4); // true

Short-circuit: In A && B, if A is false, B not evaluated.


In A || B, if A is true, B not evaluated.

7.5 Bitwise Operators

Work on individual bits of integer types.

Operator Meaning Example (for 5=0101, 3=0011)

& bitwise AND 5 & 3 = 1 (0001)

| bitwise OR 5 | 3 = 7 (0111)
Operator Meaning Example (for 5=0101, 3=0011)

^ bitwise XOR 5 ^ 3 = 6 (0110)

~ bitwise complement ~5 = -6 (two’s complement)

<< le shi 5 << 1 = 10 (1010)

>> right shi (signed) 5 >> 1 = 2 (0010)

>>> unsigned right shi always shi s zeros into MSB

Example:

java

int a = 5; // 0101

int b = 3; // 0011

[Link](a & b); // 1

[Link](a | b); // 7

[Link](a ^ b); // 6

[Link](a << 1); // 10

7.6 Condi onal (Ternary) Operator

variable = (condi on) ? value_if_true : value_if_false;

java

int age = 20;

String status = (age >= 18) ? "Adult" : "Minor";

8. Expressions

An expression is a combina on of variables, operators, and method calls that evaluates to a single
value.

Examples:

java

int sum = 10 + 20; // arithme c expression

boolean res = (x > y); // rela onal expression


int max = (a > b) ? a : b; // condi onal expression

Operator Precedence (highest to lowest):

1. Pos ix: expr++, expr--

2. Unary: ++expr, --expr, +, -, !, ~

3. Mul plica ve: *, /, %

4. Addi ve: +, -

5. Shi : <<, >>, >>>

6. Rela onal: <, >, <=, >=, instanceof

7. Equality: ==, !=

8. Logical AND: &

9. Logical XOR: ^

10. Logical OR: |

11. Condi onal AND: &&

12. Condi onal OR: ||

13. Ternary: ? :

14. Assignment: =, +=, -=, etc.

Associa vity: Most are le -to-right, except assignment (right-to-le ).

9. Solved Examples

Example 1: Command line argument sum

java

public class SumArgs {

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

int a = [Link](args[0]);

int b = [Link](args[1]);

[Link]("Sum = " + (a + b));

Run: java SumArgs 5 7 → Output: Sum = 12

Example 2: Using bitwise AND to check even/odd

java
int num = 10;

if ((num & 1) == 0)

[Link]("Even");

else

[Link]("Odd");

Example 3: Increment operators

java

int x = 5;

int y = x++ + ++x;

[Link](x + " " + y);

// Step: x=5, y=5 + (++6 → 7) = 12, final x=7 → Output: 7 12

You might also like