To find out whether two strings are equal or not, in terms of the actual characters within the strings.
What is the best way to do this?
Select 1 option
equals()
method. equalsIgnoreCase()
method. Correct Option is : A
A. use String's equals method.
For example:
String x1 = "a"; String x2 = new String ("a");
x1.equals (x2) will return true.
Because even though x1 and x2 are pointing to different objects, the content of the objects are same, which is what String's equals method checks.
x1 == x2 will return false, because == only checks if the two references are pointing to the same object or not. In this case, they are not.
For Option B. If you use this method, "a" will be considered equal to "A", which is not what the question is asking for.
For Option C. == checks for the equality of the references and not for the equality of the objects themselves.
This will return true only if two string references are pointing to the same String object, which is not what the question is asking for.
For Option D. There is no method named match in String class.
There is a matches()
method, which checks whether the String matches a regular expression.
public boolean matches (String regex)