Comparing String Objects with equals Methods
String Methods
We've compiled some helpful String methods below.
 |
Methods that are part of the AP Computer Science A Java subset are denoted by the APCSA label. |
A complete list of String methods can be found in the String API.
boolean equals(Object anObject)
This method returns
true if the value of this String is the same as the String value of anObject. NOTE: All classes extend the Object class, meaning a String is an Object.
false if the value of this String is not the same as theString value of anObject.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
value1 and value2.
- Run the code to determine whether your predictions are correct.
value1 is assigned the value false since one of the words starts with an uppercase letter and the other starts with a lowercase letter.
value2 is assigned the value true since they are the same word with all matching letters in the same case.
boolean equalsIgnoreCase(String anotherString)
This method is similar to the equals method, but doesn't distinguish between uppercase and lowercase letters.
This method returns
true if the value of this String is the same as the value of anotherString, while ignoring the difference between upper and lowercase letters.
false if the value of this String is not the same as the Value of anotherString, while ignoring the difference between upper and lowercase letters.
Your Turn
Let's try it in the Java Playground.
- Predict the values for
value1 and value2
value1 is assigned the value true since the difference in uppper and lowercase letters to start the words is ignored.
value2 is assigned the value true since they are the same word with all matching letters in the same case.
Complete List of String Learn Tutorials
Resources
Next Learn Tutorial