Wednesday, March 29, 2017

Difference between == operator and .equals() method


In this general we can use == operator for reference comparison (address comparison) and .equals() method for content comparison
Ex: - String s1=new String (“raja”);
package com.javahighq.test;


public class Test {
public static void main(String[] args) {
      String str=new String("raja");
      String str2=new String("raja");
      System.out.println(str==str2);//false
      System.out.println(str.equals(str2));//true
}
}
Note :- for any object reference str,
Str==null is always false
But null == null is always true
Ex :- String s=new String(“raja”)
System.out.println(s==null);//false
System.out.println(null==null);//true

No comments:

Post a Comment