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
trueif the value of thisStringis the same as theStringvalue ofanObject. NOTE: All classes extend theObjectclass, meaning aStringis anObject.falseif the value of thisStringis not the same as theStringvalue ofanObject.
Your Turn
Let's try it in the Java Playground.
- Predict the value of
value1andvalue2. - Run the code to determine whether your predictions are correct.
value1is assigned the valuefalsesince one of the words starts with an uppercase letter and the other starts with a lowercase letter.value2is assigned the valuetruesince 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
trueif the value of thisStringis the same as the value ofanotherString, while ignoring the difference between upper and lowercase letters.falseif the value of thisStringis not the same as the Value ofanotherString, while ignoring the difference between upper and lowercase letters.
Your Turn
Let's try it in the Java Playground.
- Predict the values for
value1andvalue2
value1is assigned the valuetruesince the difference in uppper and lowercase letters to start the words is ignored.value2is assigned the valuetruesince they are the same word with all matching letters in the same case.
Complete List of String Learn Tutorials
- Learn: Declaring Strings
- Learn: Constructing a String
- Learn: String Indexing
- Learn: Comparing Strings with compareTo methods
- Learn: Comparing Strings with equals methods
- Learn: Creating Substrings
- Learn: Creaing a new String with the Case Changed
Resources
- Learn: Declaring Strings
- Learn: Introduction to Arithmetic Expressions
- Learn: Introduction to Console Input and Output
- Learn: Formatting Output with Escape Sequences and Text Blocks
- Practice: Evaluating Expressions that Use String Methods
- Practice: Write Code that Uses the String Methods
- Practice: AP Computer Science A String Free Response Questions