java.lang.Object Class
Object is the super class for all the classes
in JAVA. Object class is also known as root for hierarchy of classes. Every
class must be a direct or indirect child of Object class. Object class consists
of mandatory 11 methods of an object. When
we are creating any object that statement must be call the its own class
Constructor and this calls its super class constructor and this hierarchy goes
to top level upto it reaches java.lang.Object class constructor and this will
makes all the Object class methods to reuse.
Properties
and methods
1
.protected Object clone ()
This method is used to clone an existed
object state to newly created object.
class
A1 implements Cloneable{
int x,y;
public static void main(String[]
args) throws Exception{
A1 a1 = new A();
a1.x=5; a1.y=7;
A1 a2 = new A();
A1 a3 = (A) a1.clone();
System.out.println(a3.x+”\t”+a3.y);
}
}
2. boolean equals(Object obj)
Ex:--
class
B {
public static void main(String [] args) {
B b1 = new B();
B b2 = new B();
System.out.println(b1.equals(b2));//it
displays false
} }
3.
protected void finalize()
This method will be called by the GC on an
object when Gc determins that there are no more references to that object.
class Gc {
public static void
main(String[] args) {
Gc g = new
Gc();
for(int
i=0;i<5;i++){
new Gc();
}
System.gc();
}
protected
void finalize(){
System.out.println("finalise
called");
}
4.
class getClass()
It returns the classname of a runtime object.
class
Test{
public
static void main(String [] args) {
Test t =
new Test();
System.out.println(t.getClass());
}
}
5.
int hashCode() :- it returns the
hashcode of an object
class
Ex {
public static void main(String [] args) {
Ex e = new Ex();
System.out.println(e.hashCode());
System.out.println(e);
}
}
6.
String toString():--It represents object
in String format.
class
Ex1{
public
static void main(String[] args) {
Ex1
a1 = new Ex1();
System.out.println(a1.toString());
}
public
String toString(){
return “am an object ”;
}
}
//the bellow methods are used in multi-threading programming... discussed in latter posts
7. void wait()
8. void wait(long
millis)
9. void wait(long
millis,int nanos)
10. void notify()
11. void notifyAll()
Features of the Wrapper Classes
Garbage
collection
Since objects are dynamically allocated by
using the new operator, you might be wondering how such objects are destroyed
and their memory released for later reallocation. In some languages, such as
C++, dynamically allocated objects must be manually released by use of a delete
operator.
Java takes a different
approach; it handles deallocation for you
automatically. The technique that accomplishes this is called garbage
collection. It works like this: when no references to an object to an object
exist, that object is assumed to be no longer needed, and the memory occupied
by the object can be reclaimed. There is no explicit need to destroy objects as
in C++. Garbage collection only occurs sporadically (if at all) during the
execution of your program. It will not occur simply because one or more objects
exist that are no longer used. Furthermore, different java run-time
implementations will take varying approaches to garbage collection, but for the
most part, you should not have to think about it while writing your programs
The finalize ()
Method
It checks wheather the object reference is stored in any object
reference variable or wheather this object reference is using by any method
call is checked by finalise().If not then it intimates the JVM to delete
that specific object.
To
add a finalizer to a class, you simply define the finalize()
method. The java run time calls that method whenever it is about to recycle an
object of that class. Inside the finalize() method you
will specify those actions that must be performed before an object is
destroyed. The garbage collector runs periodically, checking for objects that
are no longer referenced by any running state or indirectly through other
referenced objects. Right before an asset is freed, the java run time calls the
finalize() method on the object.
It finalizes the state of
the object.
Gc removes unreferenced objects from memeory,
But it behaves differently for each excution of the program.It
depends.
Means: --- new gcdemo () ---unreferenced object
1. The process of
deleting or deallocating memory to objects
Is Known as Garbage Collection.
2. This gc is internally
invoked by JVM.
3. We can also invoke GC externally from our program by placing
System,.gc() method call in our program.
4. This GC invokes finalise ().
5. This finalise () checks wheather the object reference is using
by any method
Call .Then it finalises the
object state, then it intimates the gc to delete that specific object.
Ex1:--
class Gc {
public static void
main(String[] args) {
Gc g = new
Gc();
for(int
i=0;i<5;i++){
new Gc();
}
System.gc();
}
protected
void finalize(){
System.out.println("finalise
called");
}
}
Ex2:---
class TestGC {
public static void main(String[] args) {
Runtime rt = Runtime.getRuntime();
System.out.println("Available Free Memory: " +
rt.freeMemory());
for(int i=0; i<3; i++ ) {
GC1 x = new GC1(i);
}
System.out.println("Free Memory before call to gc(): " +
rt.freeMemory());
System.runFinalization();
System.gc();
System.out.println(" Free Memory after call to gc(): " +
rt.freeMemory());
}
}
class GC1 {
String str;
int id;
GC1(int i) {
this.str = new String("abcdefghijklmnopqrstuvwxyz");
this.id = i;
}
protected void finalize() {
System.out.println("GC1 object " + id + " has been
finalized.");
}
}
WRAPPER CLASSES IN JAVA
To convert primitive data typed values (or) literal values into Java classes object format
Lists the primitive types and the corresponding wrapper classes:
Primitive
|
Wrapper
|
boolean
|
java.lang.Boolean
|
byte
|
java.lang.Byte
|
char
|
java.lang.Character
|
double
|
java.lang.Double
|
float
|
java.lang.Float
|
int
|
java.lang.Integer
|
long
|
java.lang.Long
|
short
|
java.lang.Short
|
Features of the Wrapper Classes
Ø All the methods of the wrapper classes are static.
Ø The Wrapper class does not contain constructors.
Ø Once a value is assigned to a wrapper class instance it can not be changed, anymore.
Ø All the wrapper classes are declared final. That means you cannot derive a subclass from any of them.
Ex:-
Boolean b=new Boolean(true);
System.out.println(b);
Long id=new Long(1234555);
Integer age=new Integer(25);
float ab=59.67f;
Float weight=new Float(ab);
Every wrapper class consists of parseXXX() methods to convert String representation of data into its primitive type ,
Example1:-
String num="33";
int no=Integer.parseInt(num);
Example 2:-
String height="6.0";
double h=Double.parseDouble(height);
......etc
Command Line Arguments in Java
1. Like in c/c++ JAVA can also handle command line args.
2. We can give input to java prog with the help of command line args.
3. To handle command line we use “String” class or datatype in main().
String is a powerful datatype which is used all types of data.
Java application can accept any number of arguments directly from the command line. The user can enter command-line arguments when invoking the application. When running the java program from java command, the arguments are provided after the name of the class separated by space. For example, suppose a program named CmndLineArguments that accept command line arguments as a string array and echo them on standard output device.
Ex:--
class CmdLine
{
public static void main(String[] args)
{
int a= Integer.parseInt(args[0]);
int b= Integer.parseInt(args[1]);
System.out.println("sum is: "+(a+b));
}
}
Output: -
D:\core\>java CmdLine 20 30
Sum is : 50
Ex4:--
class SumDigit
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digits :"+sum);
}
}
{
public static void main(String args[])
{
int sum, i,a,d;
a = Integer.parseInt(args[0]);
sum = 0;
for(i=1;i< =10;i++)
{
d = a%10;
a = a/10;
sum=sum + d;
}
System.out.println("Sum of Digits :"+sum);
}
}
OUTPUT
D:\core\>java SumDigit 123
Sum of Digits : 6
Sum of Digits : 6
No comments:
Post a Comment