Sun-Microsystems
have provided many built classes that can be used while developing an
application. Some of important built packages are:
·
java.lang
·
java.io
·
java.awt
·
java.util
·
java.sql
·
javax.swing
·
java.rmi
In article
we will discuss features and methods provided by some of these important built
in packages in Java:
v
java.lang: This is
most important built in package provided by Sun-Microsystems which contains
java.lang.Object class and this java.lang.Object class will be parent of every
class that you define in your source either directly or indirectly. Also this
class and whole java.lang package along with its features and method is
available to use with any Java program and you need not provide any ‘import’ statement
or package information to use the features and methods in this package. It is
internally linked to every Java class by JVM internally.
Classes available in java.lang are:
java.lang.Object/System/Runtime/String/Integer/Float/Double/Character/Long/Thread/ThreadGroup
etc.
For ex. to see the available methods and features in Object
class you can use following syntax at command prompt:
Ex: javap java.lang.Object [Enter]
Interfaces available in java.lang are:
java.lang.Cloneable/CharSequence/Runnable/Comparable
v
java.lang.Object class: As said in
above paragraph it is at the highest level of Java class hierarchy and every
class written in Java will be child to this class. In other words
java.lang.Object is parent to every class in Java. If class is not extending
any other class then it is said to be direct child of java.lang.Object class
and if a class is extending the other class then it is said to be indirect
child of java.lang.Object.
Ex:
class A //A is direct subclass of java.lang.Object.
{
}
Class B extends A //B is indirect subclass of
java.lang.Object
{
}
As java.lang.Object class is super class for all the classes
in Java, inside the object class reference variable, a reference of any type
can be stored. You can see all the methods available with java.lang.Object
class by typing ‘javap’ followed by ‘java.lang.Object’ at command prompt and
you can also use these methods with your classes.
Some important methods of java.lang.Object class are:
i.
getClass(): getClass() is an instance method in Object class and it can
be invoked by using the instance of any class and it will return java.lang.class
instance of type using which it is invoked.
To get type information
about an instance you can invoke this method, which will return instance of
type with which it is invoked.
The getClass() method is
implemented in non-Java language and it cannot be overridden as it has modifier
‘final’.
ii.
hashCode(): Internally JVM assigns integer values to every instance
created by it, and these integer values are called as hashCode.
JVM will use these unique
integer values to differentiate between the instances. This method is also
implemented in non-Java language and it is non-final method which means it can
be overridden by a programmer to use it effectively. If you are overriding this
method then you must follow some rules:
ü The hasCode
method should return same value for multiple invocations using the same object.
ü If equals()
method of object class is returning true value for two objects then their
hashCode value must be same.
ü If hasCode
value for two objects is returning true then it is not mandatory that their
hashCode value must be distinct.
iii.
equals(Object): This method is commonly used to compare the two
objects in Java, on the basis of reference. This method internally uses ‘==’
operator to compare the two objects. It should be used for the comparison of
content so it is recommended to override the equals() method and to provide the
implementation for comparison of content. If both objects are same on the basis
of content then it should return ‘true’ otherwise ‘false’ value should be
returned.
iv.
toString(): This method returns
the string representation of the object for which it is invoked.
The string representation
includes class name followed by ‘@’ symbol and it is followed by
HexStrignRepresentation of hashcode of object with which ‘toString() ‘ is
invoked.
toString() can be overridden to
return the state of object. In collection framework container classes overrides
this method and returns elements present inside the collection.
0 comments:
Post a Comment