Chapter 2
Chapter 2
Definition of Class
Inner Classes, Nested Classes, Local Classes, Anonymous Classes & Introduction to
Package
Garbage Collection [ finalize( ) Method]
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.
Access Specifiers/Modifiers
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());
}
}
StringBuffer objects are like String class objects, but they can be modified to perform manipulating
operations on the StringBuffer
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.
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.
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.
Primitive Wrapper
Data type Class
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
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.
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.
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.
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.
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.
Garbage Collection is process of reclaiming the runtime unused memory automatically. In other
words, it is a way to destroy the unused objects.
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.