Jump to content

Going Back To Basics Again, Equals Method In Java


Recommended Posts

Posted

public class ObjectComparator {

	public static void main(String []args)
	{
		Integer a = new Integer(5);
		Integer b = new Integer(5);
		
		System.out.println(a.equals(b));
		
		String abc="venu";
		String bcd="venu";
		String a1= new String("venu1");
		
		System.out.println(abc.equals(bcd));
		System.out.println(abc.equals(a1));
		
		
	}
}

out put 

true
true
false
 
 
 
Read theory below

Object class provides two methods hashcode() and equals() to represent the identity of an object. It is a common convention that if one method is overridden then other should also be implemented.

Before explaining why, let see what the contract these two methods hold. As per the Java API documentation:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashcode() method must consistently return the same integer, provided no information used in equals() comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
  • It is NOT required that if two objects are unequal according to the equals(Java.lang.Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

 

 

I have written how the hashcode values will be popluated in this URL

http://iforerunner.com/sunjava/posts/list/0/3156.page#16815

 

Posted

good job sHa_clap4  S%Hi

Posted

java ante jailor mama vachestadu

edo manaki koodu gudda anni java ne kada mari 

 

@3$%

Posted

how equalsignorecase works?

 

both will be converted to one form? 

 

will that take extra  time / memory ??

 

Posted

how equalsignorecase works?

 

both will be converted to one form? 

 

will that take extra  time / memory ??

 

equals and equalsignorecase rendu okela work chesthay implementation perspective theskunte.. jus that equalsignorecase ingnores string case

 

equalsignorecase extra time/memory em theskodhu because in both the cases, method goes and check String references not actual Strings.

 

1) First string references check chesthadhi

2) then strings length

3) then string characters

 

then it will confirm whether both the strings are equal or not ani .. so two methods same amount of time/memory theskuntay .. 

 

hope u r clear ... 

Posted

Java and .net oke talli ki puttinna kavala pillalu.....

agreed ..

 

Just like littlemoon and me :D

Posted

public class ObjectComparator {

	public static void main(String []args)
	{
		Integer a = new Integer(5);
		Integer b = new Integer(5);
		
		System.out.println(a.equals(b));
		
		String abc="venu";
		String bcd="venu";
		String a1= new String("venu1");
		
		System.out.println(abc.equals(bcd));
		System.out.println(abc.equals(a1));
		
		
	}
}

out put 

true
true
false
 
 
 
Read theory below

Object class provides two methods hashcode() and equals() to represent the identity of an object. It is a common convention that if one method is overridden then other should also be implemented.

Before explaining why, let see what the contract these two methods hold. As per the Java API documentation:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashcode() method must consistently return the same integer, provided no information used in equals() comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(object) method, then calling the hashCode() method on each of the two objects must produce the same integer result.
  • It is NOT required that if two objects are unequal according to the equals(Java.lang.Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

 

 

I have written how the hashcode values will be popluated in this URL

http://iforerunner.com/sunjava/posts/list/0/3156.page#16815

 

idea.gif?1290057832

Posted

agreed ..

 

Just like littlemoon and me :D

 

Bunny-5_0.gif?1344496926

Posted

agreed ..

 

Just like littlemoon and me :D

good good...db lo classmates,friends...friends friends undadadam chusam kaani...ila oke family members undadam first of its kind anukunta....

×
×
  • Create New...