Advanced String Handling

There are several other ways you can examine a string variable and change its value. These advanced features are possible because strings are objects in the Java language. Working with strings develops skills you’ll use on other objects later.

Comparing Two Strings

One thing you are testing often in your programs is whether one string is equal to another. You do this by using equals() in a statement with both of the strings, as in this example:

String favorite = "piano";String guess = "ukulele";System.out.println("Is Ada's favorite instrument a " + guess + "?");System.out.println("Answer: " + favorite.equals(guess));

This example uses two different string variables. One, favorite, stores the name of Ada’s favorite instrument: ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.