0% found this document useful (0 votes)
15 views7 pages

Java Unit 1

Uploaded by

anil kasula
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)
15 views7 pages

Java Unit 1

Uploaded by

anil kasula
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/ 7

Course 5: Object Oriented Programming using Java

BSc III Semester


UNIT-I
OOPs Concepts and Java Programming: Introduction to Object-Oriented concepts, proceduraland
object-oriented programming paradigm
Java programming: An Overview of Java, Java Environment, Data types, Variables, constants,
scope and life time of variables, operators, type conversion and casting, Accepting Input from the
Keyboard, Reading Input with Java.util.Scanner Class, Displaying Output with System.out.printf(),
Displaying Formatted Output with String.format(), Control Statements

Unit: OOPs Concepts and Java Programming

1. Introduction to Object-Oriented Concepts

What is Object-Oriented Programming (OOP)?


OOP is a programming paradigm that uses objects and classes to design and develop software.
• Object: A real-world entity with state (attributes) and behavior (methods).
• Class: A blueprint or template to create objects.

Benefits of OOP
• Modularity
• Reusability
• Data Hiding
• Extensibility
• Maintainability

2. Key OOP Principles

Principle Description

Encapsulation Bundling data and methods together; hiding internal state.

Abstraction Showing only relevant details and hiding complexity.

Inheritance One class (child) can inherit features of another class (parent).

Performing a single action in different ways (e.g., method overloading and


Polymorphism
overriding).

3. Procedural Programming Paradigm

Definition:
Course 5: Object Oriented Programming using Java

BSc III Semester


A programming paradigm where programs are built using procedures or functions.

Features:
• Focuses on functions.
• Code executes in a top-down approach.
• Example languages: C, Pascal.

Limitations:
• Less support for code reuse.
• Difficult to manage in large projects.
• Poor real-world modeling.

4. Object-Oriented Programming Paradigm

Definition:
A paradigm that models software as a collection of objects which interact with each other.

Features:
• Focus on objects and data.
• Better for modeling complex systems.
• Suitable for large and scalable applications.

Example Languages:
• Java, C++, Python, C#

5. Difference: Procedural vs. Object-Oriented Programming

Feature Procedural Programming Object-Oriented Programming

Approach Function-based Object-based

Data Access Global access to data Data hidden via encapsulation

Modularity Less modular High modularity

Code Reuse Difficult Easy through inheritance

Security Low High due to data hiding

6. Java and OOP

Why Java is an OOP Language:


• Everything in Java is based on classes and objects.
• Java supports all OOP principles.
Course 5: Object Oriented Programming using Java

BSc III Semester


• Example: A simple Java class
class Student {
// Attributes
String name;
int age;
// Method
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // Object creation
s1.name = "Anil";
s1.age = 21;
s1.display();
}
}
Here are comprehensive Java Programming notes for degree students covering:
An Overview of Java, Java Environment, Data Types, Variables, Constants, Scope and Lifetime
of Variables, Operators, Type Conversion and Casting, Accepting Input, Output, Control
Statements

Java Programming Notes – Introduction Unit

1. Overview of Java
• Java is a high-level, object-oriented programming language developed by Sun Microsystems
(now owned by Oracle).
• Platform Independent – thanks to Java Virtual Machine (JVM).
• Write Once, Run Anywhere – Java bytecode runs on any machine with JVM.

Key Features of Java


Course 5: Object Oriented Programming using Java

BSc III Semester


• Simple
• Object-Oriented
• Platform Independent
• Secure
• Robust
• Multithreaded
• Distributed

2. Java Environment
• JDK (Java Development Kit) – Includes compiler, debugger, and tools.
• JRE (Java Runtime Environment) – Contains JVM + libraries to run Java apps.
• JVM (Java Virtual Machine) – Converts bytecode to machine code.

3. Java Data Types

Primitive Data Types

Type Size Example

byte 1 byte byte b = 100;

short 2 bytes short s = 2000;

int 4 bytes int i = 50;

long 8 bytes long l = 9999L;

float 4 bytes float f = 10.5f;

double 8 bytes double d = 99.99;

char 2 bytes char c = 'A';

boolean 1 bit boolean flag = true;

4. Variables, Constants

Variable:
A container to store data.
Example:
int age = 20;
Course 5: Object Oriented Programming using Java

BSc III Semester


Constant:
Fixed value, declared using final keyword.
Example:
final double PI = 3.14;

5. Scope and Lifetime of Variables

Scope Type Description

Local Declared inside methods or blocks. Exists during execution.

Instance Non-static, exists with object.

Class (Static) Belongs to class, accessed without object.

6. Operators in Java

Types:
• Arithmetic: +, -, *, /, %
• Relational: ==, !=, >, <, >=, <=
• Logical: &&, ||, !
• Assignment: =, +=, -=, *=, /=
• Increment/Decrement: ++, --
• Ternary: condition ? true : false

7. Type Conversion and Casting

Implicit Type Conversion:


int a = 10;
double d = a; // Automatic conversion

Explicit Type Casting:


double d = 9.7;
int i = (int)d; // Manual casting

8. Accepting Input from the Keyboard


Use Scanner class from java.util package.
Course 5: Object Oriented Programming using Java

BSc III Semester


import java.util.Scanner;

public class InputExample {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter name: ");
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
System.out.println("Hello " + name + ", Age: " + age);
}
}

9. Displaying Output

Using System.out.printf()
int x = 10;
System.out.printf("Value is: %d\n", x);

Using String.format()
String msg = String.format("Hello %s, Age: %d", "Anil", 20);
System.out.println(msg);

10. Control Statements in Java

Conditional Statements
if (condition) {
// code
} else if (...) {
// code
} else {
// code
}
Course 5: Object Oriented Programming using Java

BSc III Semester


Switch Statement
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
default: System.out.println("Invalid Day");
}

Looping Statements
• for loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
• while loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
• do-while loop
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);

You might also like