Unit-3 Java as Object Oriented Programming Language-Overview
Unit-3 Java as Object Oriented Programming Language-Overview
09-04-2025 1
Contents:
• Fundamentals of JAVA, Arrays: one dimensional array, multi-
dimensional array, alternative array declaration statements,
• String Handling: String class methods, Classes and Methods: class
fundamentals, declaring objects, assigning object reference variables, adding
methods to a class, returning a value, constructors, this keyword, garbage
collection, finalize() method, overloading methods, argument passing, object
as parameter, returning objects, access control, static, final, nested and inner
classes, command line arguments, variable - length arguments.
4
3.1 Fundamentals of Java
❑ What is Java?
• Java is a high-level, general-purpose, object-oriented, and secure programming language
developed by James Gosling at Sun Microsystems, Inc. in 1991.
• It is formally known as OAK. In 1995, Sun Microsystem changed the name to Java. In
2009, Sun Microsystem takeover by Oracle Corporation.
❑ Editions of Java:
Each edition of Java has different capabilities. There are three editions of Java:
Java Standard Editions (JSE): It is used to create programs for a desktop computer.
Java Enterprise Edition (JEE): It is used to create large programs that run on the server
and manages heavy traffic and complex transactions.
Java Micro Edition (JME): It is used to develop applications for small devices such as set-
top boxes, phone, and appliances.
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 5
Department Of Computer Engineering
3.1 Fundamentals of Java
❑ Types of Java Applications
There are four types of Java applications that can be created using Java programming:
• Standalone Applications: Java standalone applications uses GUI components such as
AWT, Swing, and JavaFX. These components contain buttons, list, menu, scroll panel,
etc. It is also known as desktop alienations.
• Enterprise Applications: An application which is distributed in nature is called
enterprise applications.
• Web Applications: An applications that run on the server is called web applications.
We use JSP, Servlet, Spring, and Hibernate technologies for creating web applications.
• Java Platform is a collection of programs. It helps to develop and run a program written
in the Java programming language.
• Java Platform includes an execution engine, a compiler and set of libraries. Java is a
platform-independent language.
• Simple: Java is a simple language because its syntax is simple, clean, and easy to
understand.
• Object-Oriented: In Java, everything is in the form of the object. It means it has some
data and behavior. A program must have at least one class and object.
• Secure: Java is a secure programming language because it has no explicit pointer and
programs runs in the virtual machine. Java contains a security manager that defines the
access of Java classes.
class DemoFile
{
public static void main(String args[])
{
System.out.println("Hello!");
System.out.println("Java");
}
}
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 12
Department Of Computer Engineering
3.1 Fundamentals of Java
• Step 2:
Open Command Prompt.
Step 3:
Set the directory in which the .java file is saved. In our case, the .java file is saved in
C:\\demo.
javac DemoFile.java
Use the following command to run the Java program:
java DemoFile
• For example, the score of a series of football matches can be stored in a one-dimensional
array.
int Array[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 18
Department Of Computer Engineering
3.1.1 One Dimensional Array
Construction of one-dimensional array in Java
• There are mainly two ways to create an array in java :
• We can declare and store the values directly at the time of declaration :
• int marks[ ] = { 90, 97, 95, 99, 100 };
• JVM(Java Virtual Machine that drives the Java Code, converts
Java bytecode into machine language) will
assign five contiguous memory locations to
store the values assigned to the array.
Example:
//Java Program to illustrate the use of declaration, instantiation and initialization of Java array
in a single line
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration, instantiation and initialization //printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}
}
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 22
Department Of Computer Engineering
3.1.1 One Dimensional Array
Output:
33
3
4
5
• Since a 2D array consists of rows and columns, we need two indices, one to refer rows
and the other to a particular column in that row.
• Syntax:
• DataType[][] ArrayName;
• Hence, to create a two-dimensional array object we need to use the new keyword as
shown below:
• Syntax
• // Declaring 2D array
DataType[][] ArrayName;
• // Creating a 2D array
ArrayName = new DataType[r][c];
• //Declaring 2D array
int[ ][ ] a;
• //Creating a 2D array
a = new int[3][3];
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 26
Department Of Computer Engineering
3.1.2 Two Dimensional Array
• Java Two Dimensional Array of Primitive Type
Arrays are a collection of elements that have similar data types. Hence, we can create an
array of primitive data types as well as objects.
• This alternative declaration form offers convenience when declaring several arrays
at the same time:
33
3.2 String Handling:
• In Java, string is basically an object that represents sequence of char values.
char[] ch={‘w’,’e’,’l’,’c’,’o’,’m’,’e’};
String s=new String(ch);
is same as:
String s=“welcome";
• Java String class provides a lot of methods to perform operations on strings such as
compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring()
etc.
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 34
Department Of Computer Engineering
3.2 String Handling:
• String Example:
public class StringExample{
public static void main(String args[])
{
String s1="java";//creating string by Java string literal
char ch[]={'s','t','r','i','n','g','s'};
String s2=new String(ch);//converting char array to string
String s3=new String("example");//creating Java string by new keyword
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
} UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 35
Department Of Computer Engineering
3.2 String Handling:
• Output:
java
strings
example
• The length of the Java string is the same as the Unicode code units of the string.
Java String length() method example
Example:
public class strlen
{
public static void main(String args[])
{
String s1="javatpoint";
String s2="python";
System.out.println("string length is: "+s1.length());//10 is the length of javatpoint string
System.out.println("string length is: "+s2.length());//6 is the length of python string
}
}
Output:
string length is: 10
string length is: 6
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 37
Department Of Computer Engineering
3.2.2 String Concatanation
• In Java, String concatenation forms a new String that is the combination of multiple strings.
There are two ways to concatenate strings in Java:
• By concat() method
• The replace() method searches a string for a specified character, and returns a new string
where the specified character(s) are replaced.
• Syntax:
• public String replace(char searchChar, char newChar)
• For example: in real life, a car is an object. The car has attributes, such as weight and color, and
methods, such as drive and brake.
• Example:
class customer{
int id;
int age;
String name;
-----
---
public void display(){
} 23-03-2023
UNIT-III Java as Object Oriented Programming Language-Overview
48
Department Of Computer Engineering
3.3 Class & Methods
Syntax to declare a class:
class <class_name>{
field;
method;
}
• It gets memory at runtime when an object or instance is created. That is why it is known as
an instance variable.
• Predefined Method:
In Java, predefined methods are the method that is already defined in the Java class libraries is
known as predefined methods.
It is also known as the standard library method or built-in method. We can directly use
these methods just by calling them in the program at any point.
• User-defined Method
The method written by the user or programmer is known as a user-defined method. These
methods are modified according to the requirement.
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 50
Department Of Computer Engineering
3.3 Class & Methods
• Addition.java
public class Addition
{
public static void main(String[] args)
{
int a = 19;
int b = 5;
//method calling
int c = add(a, b); //a and b are actual parameters
System.out.println("The sum of a and b is= " + c);
}
• In Java, an object is created from a class. We have already created the class named Main,
so now we can use this to create objects.
• To create an object of Main, specify the class name, followed by the object name, and use
the keyword new:
• Example:
Sample myObj = new Sample();
• A return type may be a primitive type like int, float, double, a reference
type or void type(returns nothing).
• The syntax of a return statement is the return keyword is followed by the value to be
returned.
• Syntax:
return returnvalue;
• At the time of calling constructor, memory for the object is allocated in the memory.
• Every time an object is created using the new() keyword, at least one constructor is called.
Syntax:
class Employee //class name
{
Employee() //constructor
{
//body of constructor
}
}
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 61
Department Of Computer Engineering
3.7 Constructor
• In Java, a constructor is a block of codes similar to the method. It is called when an instance of
the class is created.
• At the time of calling constructor, memory for the object is allocated in the memory.
• Every time an object is created using the new() keyword, at least one constructor is called default
constructor.
Syntax:
class Employee //class name
{
Employee() //constructor
{
//body of constructor
}
} 23-03-2023 UNIT-III Java as Object Oriented Programming Language-Overview
62
Department Of Computer Engineering
3.7 Constructor
• Types of Java constructors
There are two types of constructors in Java:
✓ Default constructor (no-arg constructor).
✓ Parameterized constructor.
✓ Copy Constructor.
• Syntax:
public class Sample // Create a Class
{
int j;
sample(int i)// Parametrized Constructor
{
this.i = i;
}
}
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 64
Department Of Computer Engineering
3.7 Constructor (Parameterized Constructor)
Sample Program for Parameterized Constructor:
class Student4
{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);
}
• Syntax:
class_name (class_name ref) //Copy Constructor
{
//code
}
• Java programs compile to bytecode that can be run on a Java Virtual Machine, or JVM for
short.
• In processing reclaiming the runtime unused memory automatically. It is way to other destroy the
unused objects.
• To do so we are using free( ) function in c language. And delete( ) in c++ Language. But in java
performed automatically. So java provides better memory management.
• Mark: The garbage collector scans the heap memory segment and marks all the live
objects—that is, objects to which the application holds references. All the objects that have
no references to them are eligible for removal.
• Sweep: The garbage collector recycles all the unreferenced objects from the heap.
• Compact: The sweep step tends to leave many empty regions in heap memory, causing
memory fragmentation. Therefore, the compact phase helps arrange the objects into the
contiguous blocks at the start of the heap.
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 78
Department Of Computer Engineering
3.8.1 finalize( ) Method
• The finalize() method is invoked each time before the object is garbage collected. This
method can be used to perform cleanup processing. This method is defined in Object class
as:
protected void finalize()
{
}
• gc() method
The gc() method is used to invoke the garbage collector to perform cleanup processing. The
gc() is found in System and Runtime classes.
Call by value
• Call by Value means calling a method with a parameter as value. Through this, the
argument value is passed to the parameter.
Call by Reference:
• Java uses only call by value while passing reference variables as well. The parameter
passing by reference allows to change the values after the function call.
• But use of variables as a parameter does not allow to pass the parameter.
• The first parameter is an Data object. If you pass an object as an argument to a method, the
mechanism that applies is called pass-by-reference.
• Syntax:
Data_type_name_of _method(object_name)
{
//body of method.
}
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 88
Department Of Computer Engineering
3.11 Object as a Parameter.
Example:
class objdemo
{
int height;
int width;
objdemo(int h,int w)
{
height=h;
width=w;
}
We can change the access level of fields, constructors, methods, and class by applying the
access modifier on it.
• Default: The access level of a default modifier is only within the package. It cannot be
accessed from outside the package. If you do not specify any access level, it will be the
default.
• Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
• Public: The access level of a public modifier is everywhere. It can be accessed from
within the class, outside the class, within the package and outside the package.
class A
{
void msg()
{
System.out.println("Try it");
}
}
//save by DefE1.java
class DefE1 extends A {
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 99
Department Of Computer Engineering
3.12 Access Control
void msg()
{ System.out.println("Try to access the overridden method");
}
public static void main(String[] args) {
DefE1 p=new DefE1();
p.msg();
}
}
Output:
Try to access the overridden method
Syntax:
return_type method_name(data_type... variableName)
{ }
Example:
class Test1
{
public static void main(String args[])
{
System.out.println("Sum:"+display(20,30));
UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 105
Department Of Computer Engineering
System.out.println("Sum:"+display(20,30,40));
3.16 Variable Length Argument
System.out.println("Sum:"+display(10,20,30,40,50));
}
public static int display (int ... val)
{
int total=0;
for(int x:val)
total+=x;
return total;
}
}
Output:
Sum:50
Sum:90 UNIT-III Java as Object Oriented Programming Language-Overview
23-03-2023 106
Department Of Computer Engineering
Sum:150