Strings and Object References in Java

13. The Length of a String


Answer:

No. The String objects don't change. The reference variable ring is changed in the third
statement to refer to a different String than originally. (Its original String becomes garbage,
which is collected by the garbage collector.)

The Length of a String

The length of a string is the number of characters it contains, including space characters and punctuation.

An empty string has length zero. The length() method of a String returns its length:

public int length(); 

The method takes no parameters, but the () is needed when it is used.

String
length
"element"
7
"x"
1
""
0
"Look Homeward"
13
" Look Out "
13


Question 13:

Inspect the following code:

    String stringA = "Alphabet " ; String stringB = "Soup" ; String stringC = stringA + stringB; System.out.println( stringC.length() ) ;

What does the code print?