Module 3
Module 3
Module 3
OBJECT CLASS:
1. It is an inbuilt java library class.
2. The object class is present in the “java.lang” package.
3. It is the super most class in java and every class is either directly or indirectly a
child of object class.
4. It defines some of the most common methods needed by all java programs.
METHODS OF OBJECT CLASS:
1.toString()
2.hashCode()
3.equals()
4.wait()
5.wait(long a)
6.wait(long a, long b)
7.notify()
8.notifyAll()
9.finalize()
10.clone()
11.getClass()
toString():
1. Method declaration:
public String toString()
2. The toString() method used to get string representation of the object .
String representation: fully qualified Class name + @ + hexadecimal pseudo address.
3. programmer can override the toString() method and change the
implementation.
hashCode():
1. Method declaration: public int hashCode()
2. The hashCode() method used get unique integer ID of the object assigned by
JVM.
3. Programmer can override the hashCode() method and change the
implementation.
4. This integer ID is the decimal representation of the hexadecimal pseudo
address.
equals():
1. Method declaration: public boolean equals(object obj)
2. The equals() method is used to compare the content between two object.
3. We have to override this method to perform equality comparison like
using “==” operator.
Cloned object
Original object Object class
GetClass():
1. Method declaration: public final String getClass()
2. The getClass() method return the fully qualified class name of the
current instance.
Exception handling:
1. What is exception?
an exception is an unexpected event that occurs at the runtime
and causes the program to terminate abnormally.
2. It will be happened Scenario like:
1. Dividing a number by zero.
2. Accessing member with null reference
3. Trying to reach an invalid array index etc.
Type of exception:
In java exceptions are widely classified into two categories.
1. Checked exceptions:
1. looking at the source code if compiler can detect that code is risky and may cause
an exception it is called a checked exception.
2. Since it is detected, so It is mandatory to handle the checked exception
eg: IOException, ClassNotFoundException etc,
1. Unchecked exception:
1. Looking at the source code if compiler cannot detect that code is risky and may
cause an exception it is called a unchecked exception.
2. since it is not detected so it is not mandatory to handle the unchecked exceptions.
eg:ArithmeticException, NullPointerException etc.
1. When exception occurs the object is created by the JVM to
represent the exception.
2. There is inbuilt library class to represent each and every Exception
in java.
3. The JVM then expects at the programmer to handle the exception.
4. If the programmer fails to handle the exception JVM call the default
handler.
Exception hierarchy:
Object
Throwable
Exception
1. Byte 1. parseByte()
2. Short 2. parseShort()
3. Integer 3. parseInteger()
4. Long 4. parseLong()
5. Float 5. parseFloat()
6. Double 6. parseDouble()
7. Character 7. No parser
8. Boolean 8. parseBoolean()
File handling:
1. File handling: it is the concept of manipulating the file and folder
present in the file system.
File manipulating:
1. The common file manipulation are
1. Create a file
2. Create a folder
3. Delete a folder
4. Write data to a file
5. Read data from a file
2. Java provides a feature rich file handling API
3. All java classes related to file handling are present in “java.io”
package.
File class:
1. It is an inbuilt library class
2. It is present in “java.io” package
3. File class helps the programmer to perform some basic file
manipulation.
4. Some of the files class methods are
1. exist()
2. mkdir()
3. createNewFile()
4. delete()
Writing data to a text file:
Write() Flush()
fileWriter Demo.txt
Writer.java
close()
Steps:
1. Create an instance of file writer.
2. Write data to file writer object using write() method.
3. Transfer data to physical file using flush() method.
4. Close fileWriter object by calling close() method.
Reading data from a text file:
ABC
read()
abc Reader.java
123 FileReader
Demo.text
close()
steps:
1. Create an instance of fileReader.
2. Read data from file using read() method
3. Close file reader instance using close() method.
Collection framework:
What is a collection : a collection is a group of object that are stored
together.
Collection framework contains inbuilt class and interface that helps the
programmer to store a group of object together.
And there are commonly three types of collection
1.list 2.queue 3.set
COLLECTION INTERFACE:
1. Collection Interface is the root of the collection hierarchy.
3. It is present in the java.util package.
4. It was Introduced in version 1.2.
5. All collection objects are growable in nature.
6. Collection Interface declares the most common methods that are applicable
for any collection object.
Some methods defined by the collection interface are :
1. boolean add(Object o)
2. boolean addAll(Collection c)
3. boolean remove(Object o)
4. boolean removeAll(Collection c)
5. boolean retainAll(Collection c)
6. void clear()
7. boolean contains(Object o)
8. boolean containsAll(Collection c)
9. boolean isEmpty()
10. int size()
11. Iterator iterator()
waiting wait()
start() run()
new runnable running end
sleep()
sleeping
Thread life cycle:
1. New: in this state thread is a newly created object
2. Runnable: in this state thread is ready to begin execution
3. Running: in this state thread is currently under execution
4. Waiting: in this state thread execution is paused by jvm
5. Sleeping: in this state thread execution is paused by programmer.
6. Dead: in this state the thread will be completed execution