[go: up one dir, main page]

0% found this document useful (0 votes)
18 views30 pages

Chapter 2

Chapter 2 covers the fundamentals of objects and classes in Java, including definitions of classes, access specifiers, constructors, and the use of the 'this' keyword. It also discusses String and StringBuffer classes, wrapper classes, inner classes, and the concept of garbage collection with the finalize() method. The chapter provides insights into creating and managing classes, methods, and memory management in Java.

Uploaded by

Prem Dhawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views30 pages

Chapter 2

Chapter 2 covers the fundamentals of objects and classes in Java, including definitions of classes, access specifiers, constructors, and the use of the 'this' keyword. It also discusses String and StringBuffer classes, wrapper classes, inner classes, and the concept of garbage collection with the finalize() method. The chapter provides insights into creating and managing classes, methods, and memory management in Java.

Uploaded by

Prem Dhawan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 2

Objects and Classes

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Topics

Definition of Class

Access Specifiers, Constructors and use of “this” keyword

String, StringBuffer and Wrapper Class

Inner Classes, Nested Classes, Local Classes, Anonymous Classes & Introduction to
Package
Garbage Collection [ finalize( ) Method]

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Class

A class is a logical template to create objects that share common properties and
methods.

A class is a template used to create objects and to define object data types and
methods.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Access Specifiers/Modifiers

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Access Specifiers/Modifiers

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Access Specifiers/Modifiers

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Access Specifiers/Modifiers

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Access Specifiers/Modifiers

Access Specifiers/Modifiers

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Constructors

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Constructors

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


‘this ‘ keyword

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


String

String can be created in following two ways :

String object is immutable : can not update the string.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Methods of String class

class String_op {
public static void main(String[] args) {
String college = "Modern College";
String course = new String("M.Sc Computer Applications");
System.out.println(college + " "+ course );
System.out.println("String length is " + course.length() );
System.out.println("Character at positon 5 " + course.charAt(5) );
System.out.println("String concatenation " + course.concat(college));
System.out.println("Substring " + course.substring(3,8));
System. out.println("Equals " + course.equals(college));
System. out.println("contains " + course.contains("Computer"));
System. out.println("Compare To " + course.compareTo(college));
System. out.println("Uppercase " + course.toUpperCase());
System. out.println("Lowercase " + college.toLowerCase());
String str = " java is object oriented ";
System. out.println("Trim"+str.trim());
}
}

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


StringBuffer

StringBuffer objects are like String class objects, but they can be modified to perform manipulating
operations on the StringBuffer

This property of the StringBuffer class in Java is called “mutable”

In the above image, we can see clearly that new memory is allocated in the case of String
after adding the new String to the existing String.
Ranjana S. Shevkar, Asst. Prof. Modern College, GK
StringBuffer

Synta
x: StringBuffer <object name> = new StringBuffer();

The object name is the reference of the StringBuffer class by which we can access the
many in-built methods of the StringBuffer class in Java.

Important Constructors of StringBuffer Class

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


StringBuffer

Important methods of StringBuffer class


Sr. No. Method Description
1 append(String s) It is used to append the specified string with this string. The append() method is
overloaded like append(char), append(boolean), append(int), append(float),
append(double) etc.
2 insert(int offset, It is used to insert the specified string with this string at the specified position. The insert()
String s) method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int,
float), insert(int, double) etc.
3 replace(int It is used to replace the string from specified startIndex and endIndex.
startIndex, int
endIndex, String
str)

4 delete(int It is used to delete the string from specified startIndex and endIndex.
startIndex, int
endIndex)
5 reverse() is used to reverse the string.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


StringBuffer

Important methods of StringBuffer class


Sr. No. Method Description
6 capacity() It is used to return the current capacity.
7 ensureCapacity(int It is used to ensure the capacity at least equal to the given minimum.
minimumCapacity)
8 charAt(int index) It is used to return the character at the specified position.
9 length() It is used to return the length of the string i.e. total number of characters.
10 substring(int beginIndex) It is used to return the substring from the specified beginIndex.
11 substring(int beginIndex, It is used to return the substring from the specified beginIndex and endIndex.
int endIndex)

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


StringBuffer

Difference between String and StringBuffer

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


StringBuffer

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Wrapper Class

Primitive Data Types cannot be directly used as objects hence Wrapper classes are used.
Wrapper classes are objects encapsulating primitive Java types.
primitive values needed to be manually converted to corresponding wrapper classes

Generic classes work with objects and don't support Primitives. As a result, Wrapper classes are needed as they convert
primitive data types into objects and objects are really important if we need to modify the arguments passed in a method.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Wrapper Class

Primitive Wrapper
Data type Class
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Inner Classes

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Inner Classes

In Java, just like methods, variables of a class too can have another class as its member. Writing a
class within another is allowed in Java. The class written within is called the nested class, and the
class that holds the inner class is called the outer class.

Nested classes are divided into two types −


Non-static nested classes − These are the non-static members of a class.
Static nested classes − These are the static members of a class.

Inner classes are a security mechanism in Java. We know a class cannot be associated with the
access modifier private, but if we have the class as a member of other class, then the inner class
can be made private. And this is also used to access the private members of a class.

Inner Class
Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class,
an inner class can be private and once you declare an inner class private, it cannot be accessed
from an object outside the class.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Method local Inner Classes

In Java, we can write a class within a method and this will be a local type. Like local variables, the
scope of the inner class is restricted within the method.
A method-local inner class can be instantiated only within the method where the inner class is
defined.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Anonymous Inner Classes

An inner class declared without a class name is known as an anonymous inner class. In case
of anonymous inner classes, we declare and instantiate them at the same time. Generally,
they are used whenever you need to override the method of a class or an interface.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Nested Classes

A static inner class is a nested class which is a static member of the outer class. It can be
accessed without instantiating the outer class, using other static members. Just like static
members, a static nested class does not have access to the instance variables and methods
of the outer class.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Introduction to Package

A Java package is a collection of similar types of sub-packages, interfaces,


and classes.
Types of packages in Java:
1. built-in packages
2. user-defined packages.
The package keyword is used in Java to create Java packages.
Examples of built-in packages :
3. java.util
4. java.lang
5. java.awt
6. javax.swing
7. java.net
8. java.io
9. java.sql, etc.
Package is imported using packagename.* statement.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Garbage Collector

Garbage Collection is process of reclaiming the runtime unused memory automatically. In other
words, it is a way to destroy the unused objects.

Use of Garbage Collector


•It makes java memory efficient because garbage collector removes the unreferenced objects
from heap memory.
•It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra
efforts.
Unreferencing Objects

1. By nulling the reference


2. By assigning a reference to another
3. By anonymous object etc.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK


Ranjana S. Shevkar, Asst. Prof. Modern College, GK
finalize() method
The finalize() method is invoked each time before the object is garbage collected. This method
can be used to perform cleanup processing.

protected void finalize(){}

The Garbage collector of JVM collects only those objects that are created by new keyword. To destroy the object
without new, use finalize method to perform cleanup processing

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.

public static void gc(){}

Garbage collection is performed by a daemon thread called Garbage Collector(GC). This


thread calls the finalize() method before object is garbage collected.

Ranjana S. Shevkar, Asst. Prof. Modern College, GK

You might also like