String compare by == operator in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 09-Feb-2018 12:16:15
Advertisements