In Java, we use equals() to compare String.

1. equals()

1.1 To compare Strings, uses equals()


	String name = "mkyong";

	if ("mkyong".equals(name)) {
		System.out.println("Yes");
	}

Output


Yes

1.2 The equals() is case sensitive.


	String name1 = "hello";
	String name2 = "HELLO";

	if (name1.equals(name2)) {
		System.out.println("Yes");
	} else {
		System.out.printf("NO, equals is case sensitive!");
	}

Output


NO, equals is case sensitive!

2. equalsIgnoreCase()

To perform a case insensitive compare.


	String name1 = "hello";
	String name2 = "HELLO";

	if (name1.equalsIgnoreCase(name2)) {
		System.out.println("Yes");
	} else {
		System.out.printf("NO, equals is case sensitive!");
	}

Output


Yes
Note
Don’t use this == operator to compare String, this is the most common beginner mistake. In Java == is used to compare object references, test if they are the same object.

References