Home Tutorial Java Corejava equals method in java

 
 

equals method in java
Posted on: July 5, 2010 at 12:00 AM
Advertisement
In this example you will see how to use equals method in java.

Description:

The equals method found in java.lang.object. It is use to check  state(data) of object not identifying the location in Memory of the object. It return the Boolean value.

In other way it compare content of  object. where as the "==" operator check instance of object are same or not.

Code:

public class EqualDemo1 {
  public static void main(String[] args) {
    String s1 = new String("bharat");
    String s2 = new String("bharat");
    String s3 = new String("suraj");
    if (s1.equals(s2))
      System.out.println("TRUE");
    else
      System.out.println("FALSE");
    if (s1.equals(s3))
      System.out.println("TRUE");
    else
      System.out.println("FALSE");
  }
}

Output:

Download this code

Difference between == and equals method in java
In this tutorial you will see the difference between the == and equals for comparision in java.
 
Advertisement

Related Tags for equals method in java:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 5, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com