0
0
Javaprogramming~15 mins

String creation in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is the simplest way to create a String in Java?
You can create a String by enclosing text in double quotes, like String s = "Hello";. This is called a string literal.
touch_appClick to reveal answer
intermediate
What does new String("text") do compared to a string literal?
It creates a new String object in memory, even if the same text exists as a literal. This is less efficient than using literals directly.
touch_appClick to reveal answer
beginner
Can Strings in Java be changed after creation?
No, Strings are immutable. Once created, their content cannot be changed. Any modification creates a new String object.
touch_appClick to reveal answer
intermediate
How does Java optimize String creation with literals?
Java uses a String pool to store literals. If a literal already exists, Java reuses it instead of creating a new object.
touch_appClick to reveal answer
intermediate
What is the difference between String s = "abc"; and String s = new String("abc");?
The first uses the String pool and reuses the literal if available. The second always creates a new String object in memory.
touch_appClick to reveal answer
Which of these creates a String literal in Java?
AString s = StringBuilder.append("Hello");
BString s = new String("Hello");
Cchar[] s = {'H','e','l','l','o'};
DString s = "Hello";
What happens when you create a String with new String("abc")?
AIt creates a new String object in memory.
BIt creates a StringBuilder object.
CIt reuses an existing String from the pool.
DIt throws an error.
Are Java Strings mutable (can they be changed after creation)?
ANo, Strings are immutable.
BYes, you can change characters inside a String.
COnly if you use StringBuilder.
DOnly if you use new String().
What is the purpose of the String pool in Java?
ATo store all String objects created with new.
BTo store unique String literals for reuse.
CTo speed up String concatenation.
DTo convert Strings to char arrays.
Which is more memory efficient for creating Strings?
AUsing char arrays
BUsing new String("text")
CUsing String literals like "text"
DUsing StringBuilder
Explain how Java creates and stores Strings using literals and the new keyword.
Why are Java Strings immutable and what does that mean for modifying text?