Friday, 18 June 2010

Use of javap

javap command is stand for java parsing .you can see public and protected method for a particular library class.
Take an example To see all the protected and public method of ArrayList, we have to execute below command
->javap java.util.ArrayList

output:

Compiled from "ArrayList.java"
public class java.util.ArrayList extends java.util.AbstractList implements java.util.List,java.util.RandomAccess,java.lang.Cloneable,java.io.Serializable{
    public java.util.ArrayList(int);
    public java.util.ArrayList();
    public java.util.ArrayList(java.util.Collection);
    public void trimToSize();
    public void ensureCapacity(int);
    public int size();
    public boolean isEmpty();
    public boolean contains(java.lang.Object);
    public int indexOf(java.lang.Object);
    public int lastIndexOf(java.lang.Object);
    public java.lang.Object clone();
    public java.lang.Object[] toArray();
    public java.lang.Object[] toArray(java.lang.Object[]);
    public java.lang.Object get(int);
    public java.lang.Object set(int, java.lang.Object);
    public boolean add(java.lang.Object);
    public void add(int, java.lang.Object);
    public java.lang.Object remove(int);
    public boolean remove(java.lang.Object);
    public void clear();
    public boolean addAll(java.util.Collection);
    public boolean addAll(int, java.util.Collection);
    protected void removeRange(int, int);
}

What happens when object is passed to System.out.println()

When any object is passed to  System.out.println(),  by default it calls to the toString() of
Object class. If  class overrides it then overrided toString method would be called.

Example:
Before Overriding toString method:
public class MyClass
{
public static void main(String args[])
{
MyClass myclass=new MyClass();
System.out.println(myclass);
}
}
O/P- hexadecimal number.
Reason: toString method is not overrided in MyClass. Object toString is getting called.

After Overriding toString method:
 
public class MyClass
{
public String toString()
{
return "hello";
}
public static void main(String args[])
{
MyClass myclass=new MyClass();
System.out.println(myclass);
}
}
O/P:
hello