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

Updated on: 26-Feb-2020

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements