Java String Class: What It Is and Key String Methods - Quiz

Total: 9 questions

1. What is the output? Which reference variables and String objects are created before the println? String firstStr = "first "; String secondStr = firstStr + "second "; firstStr.concat("third "); secondStr.concat(firstStr); firstStr += "fourth "; System.out.println(firstStr + " " + secondStr);

Output: first fourth first second (the double space appears because firstStr ends with a space and the println adds one more).

Two reference variables are created: firstStr and secondStr.

Eight String objects are created: "first ", "second ", "third ", "fourth " (literals in the String Pool) and "first second ", "first third ", "first second first ", "first fourth " (created at runtime).

2. Which part of memory String literals are put in?

In the String Pool (String constant pool), a special area of the heap since Java 7 (before Java 7 it was in PermGen).

3. How many objects are created by this line? String str = "abc";

One reference variable and one String object: the literal "abc" is placed in the String Pool.

4. How many objects are created by this line? String worldStr = new String("world");

Two objects: the literal "world" goes into the String Pool, and new String("world") creates another String object in the heap. worldStr refers to the heap object.

5. When can java.lang.StringBuilder and java.lang.StringBuffer be used?

When a string has to be changed many times, for example in a loop, because every change of a String creates a new object. StringBuffer is chosen when several threads share the same object.

6. How do StringBuffer and StringBuilder differ?

Both have the same API. StringBuffer is synchronized (thread-safe), while StringBuilder is not, so StringBuilder is faster in single-threaded code.

7. 

Which strings are placed in the String Pool automatically, and what does the intern() method do?

Only string literals and compile-time constants (for example, a final variable concatenated with a literal) go into the pool automatically. A string created with new or built at runtime is a new object in the heap, so == returns false for it even when the text is the same. The intern() method returns the pooled string with the same content and adds it to the pool if it is not there yet: after that, "Java" == b.intern() returns true. To compare text, still use equals().

8. 

What does the compareTo() method of String return, and why is "Java".compareTo("java") negative?

It compares strings lexicographically, by Unicode code, and returns a number: negative if the calling string is less than the argument, positive if it is greater, and 0 if they are equal. The result is the difference between the codes of the first mismatching characters; if one string is a prefix of the other, it is the difference in length ("apple".compareTo("app") = 2). The code of 'J' is 74 and the code of 'j' is 106, so the result is -32: uppercase letters come before lowercase ones. For a case-insensitive comparison use compareToIgnoreCase(); for language-aware sorting use java.text.Collator.

9. 

What is the difference between isEmpty() and isBlank()? How do you safely check that a string is not filled in?

isEmpty() returns true only if the length of the string is 0. isBlank() (Java 11) returns true if the string is empty or contains only whitespace: spaces, tabs, line breaks. So " ".isEmpty() is false, while " ".isBlank() is true. Neither method checks for null: calling them on null throws a NullPointerException, so a safe check looks like this: s == null || s.isBlank().

Page 1 of 1