Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
equal
communitycreator

What is String.equals() in Java?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The EQUALS() function takes in a string as a parameter and returns true if both the source string and parameter are equal; otherwise, it returns false.

Figure 1 shows a visual representation of the EQUALS() function.

Figure 1: Visual representation of EQUALS() function

Syntax

boolean equals(Object strObj)

string1.equals(string2)

Parameter

The EQUALS() function takes another string (string2) to be compared with string1 as a parameter.

Return value

The EQUALS() function returns true if both the strings are equal; otherwise, it returns false.

Code

class JAVA {
public static void main( String args[] ) {
String string_1 = "educative";
String string_2 = "edpresso";
String string_3 = "EDUCATIVE";
//same both strings
System.out.println("string_1.equals('educative'):");
System.out.println(string_1.equals("educative"));
//different both strings
System.out.println("string_2.equals('educative'):");
System.out.println(string_2.equals("educative"));
//same string but one is uppercase and other is lowercase
System.out.println("string_3.equals('educative'):");
System.out.println(string_3.equals("educative"));
//string2 is not string obj
System.out.println("string_1.equals(12345):");
System.out.println(string_1.equals(12345));
}
}

RELATED TAGS

java
equal
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring