wellnwill Marathahalli Bangalore

Object class


Object class is the parent class of all the classes in java. It is present inside java.lang package. There are many methods in Object class.


Object class methods


toString() method

  • By default toString() method returns a string which represents an object.
  • By default it returns fully qualified classname@hexadecimal address.
  • In java, when we print a reference variable implicitly toString() method is invoked.
  • If our class does not contain toString() method then Object class toString() method will be executed.
public String toString()
{
// return “fully qualified classname@hexadecimal code”;
}

class Employee
{
	
String name;
int id;

Employee(String name,int id)
{
this.name=name;
this.id=id;
}

public static void main(String[] args)
{
Employee s=new Employee("Raj",20);
System.out.println(s);//System.out.println(s.toString());
}
}
Output- Employee@1db9742





class Employee1
{
	
String name;
int id;

Employee1(String name, int id)
{
this.name=name;
this.id=id;
}

public String toString()
{
 return name+" "+id;
}

public static void main(String[] args)
{
Employee1 s=new Employee1("Raj",20);
System.out.println(s);//System.out.println(s.toString());
}
}
Output-Raj  20

public boolean equals(Object obj)

  • equals() method takes an argument of object type.
  • By default equals() compares the current object and given object based on address and return a boolean true or false.
  • If we want to compare two object based on object content then we have to override equals() method in our class.
  • If our class is not overriding equals() method then object class equals() method will be executed
class Employee1
{
	
String name;
int id;

Employee1(String name, int id)
{
this.name=name;
this.id=id;
}

public static void main(String[] args)
{
Employee1 e1=new Employee1("Raj",20);
Employee1 e2=new Employee1("Dheeraj",30);
System.out.println(e1.equals(e2));
}
}
class Test
{
 public static void main(String args[])
 {
  String st1=new String("Raj");
  String st2=new String("Lucky");
  String st3=new String("Raj");
  System.out.println(st1.equals(st2));
  System.out.println(st1.equals(st3));
  System.out.println(st1);
 }
}
Output-false ,true, Raj







public int hashCode() method

  • When we create object then for every object a unique number is generated by JVM which is known as Hash code value of the object.
  • Hash code doesnot represent address of object.
  • This hash code is used by JVM while saving object into hashing related data structures like hash table, hash map, hash set etc.
  • Saving objects based on hash code make search operation easy.
  • We can override hashCode() method in our class to generate our own hash code value.
  • toString() method of Object class will internally call hashCode() method.

class Employee
{
	
int id;

Employee(int id)
{
this.id=id;
}

public int hashCode()
{
 return id;
}

public static void main(String[] args)
{
Employee e1=new Employee(100);
Employee e2=new Employee(101);
System.out.println(e1.hashCode());
System.out.println(e2.hashCode());
}
}
Output-100,101


class Employee
{
	
int id;

Employee(int id)
{
this.id=id;
}

public int hashCode()
{
 return id;
}

public static void main(String[] args)
{
Employee e1=new Employee(100);
Employee e2=new Employee(101);
System.out.println(e1);//classname@hexadecimal of 100
System.out.println(e2);
}
}
Output-Employee@64
            Employee@65


class Employee 
{
  int sal ;
  
	Employee( int sal )
	{
		this.sal = sal;
	}
	
	public boolean equals( Object obj )
	{
		Employee e2 = ( Employee )obj;
		if(this.sal == e2.sal)
		{
			return true;
		}
		else
		{
			return false;
		}
    }
}

class Test
{
	public static void main(String[] args) 
	{
		Employee e1 = new Employee(20);
		Employee e2 = new Employee(20);
                Employee e3= new Employee(22);
                System.out.println(e1.equals(e2));
		System.out.println(e1.equals(e3));
	}
}
Output-true,false
class Employee
{
int id;
Employee(int id)
{
this.id=id;
}
public int hashCode()
{
 return id;
}
public static void main(String[] args)
{
Employee e1=new Employee(100);
Employee e2=new Employee(101);
System.out.println(e1.hashCode());
System.out.println(e2.hashCode());
}
}
Output-100,101


class Employee
{
int id;
Employee(int id)
{
this.id=id;
}
public int hashCode()
{
 return id;
}
public static void main(String[] args)
{
Employee e1=new Employee(100);
Employee e2=new Employee(101);
System.out.println(e1);//classname@hexadecimal of 100
System.out.println(e2);
}
}
Output-Employee@64
       Employee@65
       

Footer with Map

Top