Home Tutorial Java Corejava Difference between == and equals method in java

 
 

Share on Google+Share on Google+
Difference between == and equals method in java
Posted on: July 5, 2010 at 12:00 AM
Advertisement
In this tutorial you will see the difference between the == and equals for comparision in java.

Description:

For comparing equality of string ,We Use equals() Method. There are two ways of comparison in java. One is "==" operator and another "equals()" method .  "==" compares the reference value of string object whereas equals() method is present in the java.lang.Object class. This method compares content of the string object. .

Code:

public class EqualDemo2 {
  public static void main(String[] args) {
    String s1 = "Hello";
    String s2 = "Hello";
    String s3 = new String("Hello");

    if (s1.equals(s2)) {
      System.out.println("TRUE");
    else {
      System.out.println("FALSE");
    }

    if (s1 == s2) {
      System.out.println("TRUE");
    else {
      System.out.println("FALSE");
    }
    if (s1.equals(s3)) {
      System.out.println("TRUE");
    else {
      System.out.println("FALSE");
    }

    if (s1 == s3) {
      System.out.println("TRUE");
    else {
      System.out.println("FALSE");
    }

  }
}

Output:

Download this code

Advertisement

Related Tags for Difference between == and 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