AP® Computer Science A glossary

compareTo

Course: AP® Computer Science A Shows up in: Unit 1: Using Objects and Methods

The String method compareTo(String other): negative if this comes first alphabetically, 0 if equal, positive if this comes later.

Appears in the course and exam description (CED): 1.15.B.2

Read a.compareTo(b) < 0 as “a comes before b”. Capital letters come before lowercase ones, but exam questions usually use one case.

System.out.println("cat".compareTo("dog") < 0);
System.out.println("pear".compareTo("pear"));

Output:

true
0
Learn it properly, with a checkpoint 1.15 String Manipulation

Related terms

Was this page helpful?

No names are ever shown, and every public comment is checked by us first. Please do not include personal details. To get a reply, email [email protected].

↑ Top

Enlarged view of the selected figure