String Indexing

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

Character 'I' (the beginning character of the string).

String Indexing

The beginning character of a string corresponds to index 0 and the last character corresponds to the index (length of string)-1.

String Indexes start at Zero

The length of a string is the number of characters it contains, including spaces, punctuation, and control characters. Fill in the following table by clicking on the buttons.

Code Value
"buttons".length()
"buttons and bows".length()
"buttons/tand/tbows".length()
"".length()

The sequence "/t" stands for a single character, a tab character. Each tab character counts as one character (although it might be displayed on a monitor or printer using several spaces).

Be careful about the difference between an empty string, and a null reference. An empty string is an object that contains no characters, and so has length 0. A null reference means there is no object present.

QUESTION 4:

What does the following code write?

String myString = null;

System.out.println("The length is: " + myString.length() )