String compare by == operator in Java



You can compare two strings using == operator. But, it compares references of the given variables, not values.

Example

Live Demo

public class Sample {
   public static void main(String args[]){
      String str1 = "hello";
      String str2 = "hello";
      System.out.println(str1 == str2);
   }
}

Output

true

Advertisements