Object Class in Java – java.lang package

After java.util package, java.lang package is another java package that is used most by the programmers. This package is by default imported in the java program, therefore it is not required to import it explicitly. Some of the most commonly used class in this package are:

  1. Object
  2. String
  3. StringBuilder
  4. StringBuffer
  5. Wrapper Classes

Now discussing these classes in detail.


Object Class

Object class encapsulates the most commonly used methods. Developers of Sun Microsystems made Object class the parent class of all the other classes in java, this made the methods of Object class available by default to every other java class. Every other class in java is the sub class of Object class, either directly or indirectly. If our class extends the Object class in our program directly then our class is the direct sub class of Object class. Otherwise if we extend any other class in our program then our class is indirect sub class of Object class. For example,

class Test1 extends Object
{
    //our code
}

Here Test1 is the direct sub class of Object.

class Test2 extends Exception
{
    //our code
}

Here, Test2 is the indirect sub class of Object.
There are 11 methods defined inside Object class. These methods are:

1) toString(): Prototype of this method is

public String toString()

Implementation of this class in Object class is as follows:

public String toString()
{
    return getClass().getName + "@" + Integer.toHexString(hashCode());
}

Note- In the above implementation you can see that toString() return the class name of the object and the hashcode’s hexadecimal string representation concatenated to it.
This method can be used to find or get the string representation of any type of object. If we try to print any object reference then internally compiler will execute toString(). In order to get our own string representation we have to override toString() in our class. This practice of overriding is highly recommended. Even other java classes like StringBuffer, Wrapper classes have toString() that returns proper string form. For example,

public class Employee
{
    String name;
    int empid;
    Employee(String name, int empid)
    {
        this.name = name;
        this.empid = empid;
    }
    public static void main(String []args)
    {
        Employee e1 = new Employee("James", 1001);
        Employee e2 = new Employee("Mark", 1002);
        System.out.println(e1);
        System.out.println(e2);
    }
}

Output of the above code will be:

Employee@2a139a55
Employee@15db9742

So in the above code when we are trying to print Employee object reference to return his name and id we need to override toString() in the following manner.

public String toString()
{
    return name;
    return name+"------"+empid;
    return "This is an employer with name:+"name"+" with id:"+empid;
}

Consider the following example in which the class has overrided toString().

public class Mall
{
    public String toString()
    {
        return "test";
    }
    public static void main(String []args)
    {
        Mall m = new Mall();
        String str = new String("Thomas");
        Integer i = new Integer(100);
        System.out.println(m);
        System.out.println(str);
        System.out.println(i);
    }
}

Output of the above code will be:

test
Thomas
100

As in the above code you can see that in the first print statement, instead of printing class name and hashcode, our program is printing the defined value i.e. “test”. And in the other 2 cases, when we give other certain objects, our program printed them in the form of strings.

Leave a Comment

Your email address will not be published. Required fields are marked *