Stage 1 · Lesson 3 of 9
Strings and equals()
String is a class, and are : once made, they never change. toUpperCase(), trim(), replace() and substring() all return a new String. The most famous Java trap is comparing strings with ==.
That checks whether two references point to the same object, not whether the text matches. Java keeps identity (the same object) apart from (the same contents). Use a.equals(b) to compare the contents.
== can seem to work on text written in your code and then fail on text read while the program runs. Get characters with charAt(i) and the length with length(). Split text with split(","), and format output with String.format("%s is %d", name, age).
When you build a string in a , use a StringBuilder, because += creates a new String on every pass.
A question to check yourself, an AI practice session and 2 cheat sheet entries come when you add the roadmap to your account. Add it
Resources for this stage
Start with· follow one of these
- CourseFreeJava Programming MOOC
University of Helsinki · Beginner
Free Helsinki course with hundreds of auto-tested exercises; parts 1 to 8 cover this stage
- GuideFreeDev.java: Learn Java
Oracle · Beginner
Official tutorials from the Java team: read the language basics path next to the MOOC