Completion requirements
The String class is used for text manipulation. As you read, you will learn different ways to create Strings, methods to manipulate Strings, the String concatenation operator '+', and about how Strings are immutable.
13. The Length of a String
Answer:
No. TheString
objects don't change. The reference variablering
is changed in the third
statement to refer to a differentString
than originally. (Its originalString
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?