Programming foundations

Stage 1 · Lesson 3 of 9

Strings and equals()

Your first language

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

  • CourseFree
    Java Programming MOOC

    University of Helsinki · Beginner

    Free Helsinki course with hundreds of auto-tested exercises; parts 1 to 8 cover this stage

  • GuideFree
    Dev.java: Learn Java

    Oracle · Beginner

    Official tutorials from the Java team: read the language basics path next to the MOOC

Practise

  • Small exercises in your language track, with free feedback from a human mentor

Keep handy

  • Quick syntax lookups and an online compiler for testing a snippet on a shared computer

Mark this stage as not done?