Object Oriented Data Structure: Unit 1
Object Oriented Data Structure: Unit 1
CST364
Course Outcomes
Unit-1
Text Books
Introduction
Object-oriented programming (OOP) is a way of thinking
about and organizing code for maximum reusability. With
this type of programming, a program comprises objects
that can interact with the user, other objects, or other
programs. This makes programs more efficient and easier
to understand.
Benefits of OOPs
Troubleshooting is easier with the OOP language
Code Reusability
Productivity
Data Redundancy
Code Flexibility
Solving problems
Security
Features of object-oriented programming
2. Importance is not given to data but to Importance is given to the data rather than
functions as well as sequence of procedures or functions because it works
actions to be done. as a real world.
5. Data can move freely from Objects can move and communicate with
function to function in the system. each other through member functions.
Difference between Procedural and OO
programming language
Java was started as a project called "Oak" by James Gosling in June 1991.
Gosling's goals were to implement a virtual machine and a language that had a
familiar C-like notation but with greater uniformity and simplicity than
C/C++.
The first public implementation was Java 1.0 in 1995. It made the promise of
"Write Once, Run Anywhere", with free runtimes on popular platforms.
What is JVM?
Java Virtual Machine (JVM) is a engine that provides runtime environment to
drive the Java Code or applications. It converts Java bytecode into machines
language. JVM is a part of Java Runtime Environment (JRE). In other programming
languages, the compiler produces machine code for a particular system. However,
Java compiler produces code for a Virtual Machine known as Java Virtual Machine.
Simple:
• User friendly Syntax
• There is automatic Garbage collection.
• No complicated features like operator overloading, pointers etc., as in C and C++.
Secured:
• Java is secured as it uses runtime environment of its own with almost no
interaction with System OS.
• The components Classloader, Bytecode Verifier and Security Manager of Java makes
it much more secure.
Independent:
Java is compiled into bytecode(an intermediate format). ● This Bytecode can be run on
any machine with Java Virtual Machine.
Features of Java
Java Program is written once and can be run that java program in any of the operating
systems Windows, Linux etc., without re-compiling.
Hence Java Program is considered as “Write Once, Run Anywhere”
This concept made Java Platform Independent.
Features of Java
Robust:
• Java has automatic Garbage Collector which helps to get rid of objects which are
not being used by a Java application anymore.
• This makes memory management stronger.
• Java emphasizes on compile time error checking and runtime error checking to
eliminate error prone codes.
• Exception handling Mechanism also makes Java Robust.
Portable:
• Java Programs can be on any platform like Windows, Linux, Mac etc.,
• Java Programs, applets can be transferred over world wide web.
• Java Compiler outputs a Byte code which is executed by Java run-time system, Java
Virtual Machine.
• Once, JVM is installed for particular system, any java program can run on it.
Dynamic :
We can develop programs in java which dynamically change on internet (e.g. Applets).
High Performance :
Along with interpreter there will be JIT( Just in Time) compiler which enhances the speed
of execution.
Multithreaded :
Executing different parts of program simultaneously is called Multithreading. This is an
essential feature to design server side program.
Distributed :
Java is designed for use on network, it has an extensive library which works in agreement
with TCP/IP.
Variables
• What is a variable?
• The name of some location of memory used to hold a data value
• Different types of data require different amounts of memory. The
compiler’s job is to reserve sufficient memory
• Variables need to be declared once
• Variables are assigned values, and these values may be changed later
• Each variable has a type, and operations can only be performed
between compatible types
Variable Names
• Valid Variable Names: These rules apply to all Java names, or identifiers,
including methods and class names
• Starts with: a letter (a-z or A-Z), dollar sign ($), or underscore (_)
• Followed by: zero or more letters, dollar signs, underscores, or digits
(0-9).
• Uppercase and lowercase are different (total ≠ Total ≠ TOTAL)
• Cannot be any of the reserved names. These are special names
(keywords) reserved for the compiler. Examples:
class, float, int, if, then, else, do, public, private, void, …
Data types
1. Primitive Data type
Primitive data are only single values and have no special
capabilities. There are 8 primitive data types. They are depicted
below in tabular format below as follows:
1. Boolean 5. Byte
2. Char 6. long
3. Int 7. float
4. Short 8. double
• Selection Statements
– If and switch
• Iteration Statements
– While, do-while, for and nested loops
• Jump Statements
– Break, continue and return
if-else
if(conditional_statement){
statement to be executed if conditions becomes
true
}else{
statements to be executed if the above condition
becomes false
}
Example
int a = 3, b = 4;
if (a >b) a =1;
else b = 1
If Else example
public class IfElseIfExample { else if(marks>=70 && marks<80){
public static void main(String[] args) { System.out.println("B grade");
int marks=65;
}
if(marks<50){ else if(marks>=80 && marks<90){
System.out.println("fail");
} System.out.println("A grade");
else if(marks>=50 && marks<60){ }else if(marks>=90 && marks<100
System.out.println("D grade"); ){
} System.out.println("A+ grade");
else if(marks>=60 && marks<70){
System.out.println("C grade");
}else{
}
System.out.println("Invalid!");
}
}
}
Switch
switch(byte/short/int){
case expression:
statements
case expression:
statements
default:
statement
}
while - loop
while(condition_statement🡪true){
Statements to be executed when the condition
becomes true and execute them repeatedly until
condition becomes false.
}
E.g.
int x =2;
while(x>5){
system.out.println(“value of x:”+x);
x++;
}
do while - loop
do{
statements to be executed at least once without
looking at the condition.
The statements will be exeucted until the condition
becomes true.
}while(condition_statement);
for - loop
for(int i=0;i<50;i++){
if(i%13==0){
break;
}
System.out.println(“Value of i:”+i);
}
return
return statement can be used to cause execution
to branch back to the caller of the method.
Access Specifiers
In Java, access modifiers are used to set the accessibility (visibility) of classes,
interfaces, variables, methods, constructors, data members, and the setter
methods.
class Animal
{
public void method1()
{…}
private void method2()
{...}
}
There are four access modifiers keywords in Java and they are:
Arrays in Java
Creating, initializing, and accessing an Array
One-Dimensional Arrays:
type var-name[];
OR
type[] var-name;
Example:
int intArray[]; //declaring arrayintArray = new int[20]; // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one
Example
Example
class GFG {
public static void main(String[] args)
{
// declares an Array of integers.
int[] arr;
// allocating memory for 5 integers.
arr = new int[5];
// initialize the first elements of the array
arr[0] = 10;
// initialize the second elements of the array
arr[1] = 20;
// so on...
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i
+ " : " + arr[i]);
Two-Dimensional Array
In Java, a two-dimensional array is stored in the form of rows and columns and is
represented in the form of a matrix.
The general declaration of a two-dimensional array is,
data_type [] [] array_name;
OR
There are three ways to access the package from outside the package.
import package.*;
import package.classname;
fully qualified name.