Topic 9.2: Working with Selected classes from the Java API - Create and manipulate Strings Flashcards
What is string interning and how does it optimize memory usage?
This is the process of storing string literals in a string pool to optimize memory usage.
When encountering a string literal, the JVM checks if it already exists in the pool. If it does, a reference to the existing string is returned. If not, a new string object is created and added to the pool.
This ensures that only one copy of each unique string exists in memory, reducing memory footprint and enhancing performance.
The equalsIgnoreCase() method can be used for this
What method can be used for case-insensitive string comparison in Java?
To achieve this you can use the substring() method
String text = "Hello, World!"; String substring = text.substring(7);
How can you extract a substring from a string in Java?
To achieve this, you can use the startsWith() method.
String text = "Hello, World!"; boolean startsWithHello = text.startsWith("Hello");
How can you check if a string starts with a specific prefix?
To achieve this you can use the toUpperCase() method
String text = "Hello, World!"; String uppercase = text.toUpperCase();
How can you convert a string to uppercase in Java?
How can you convert a string to uppercase in Java?
To achieve this you can use the toUpperCase() method
String text = "Hello, World!"; String uppercase = text.toUpperCase();
How can you create strings in Java using string literals or the new keyword?
String literals involve enclosing characters within double quotation marks, such as “Hello, world!”. The new keyword can be used with the String class constructor, as in new String(“John”).
To achieve this you can use the trim() method
String text = " Hello, World! "; String trimmed = text.trim();
How can you remove leading and trailing whitespace from a string in Java?
How can you check if a string starts with a specific prefix?
To achieve this, you can use the startsWith() method.
String text = "Hello, World!"; boolean startsWithHello = text.startsWith("Hello");
How can you search for the first occurrence of a substring within a string in Java?
To achieve this you can use the indexOf() method, which takes the substring as a parameter and returns the index of the first occurrence. If the substring is not found, it returns -1.
For example, given the string “Hello, World!”, calling indexOf(“o”) would return 4.
String text = "Hello, World!"; int index1 = text.indexOf("o"); int index2 = text.indexOf("XYZ");
To achieve this you can use the replace() method with a single character as the target and the replacement value
For example, given the string “Hello, World!”, calling replace(‘o’, ‘e’) would return “Helle, Werld!”.
String text = "Hello, World!"; String replaced = text.replace('o', 'e');
How can you replace a character within a string in Java?
How can you find the index of the last occurrence of a specific substring within a string?
To achieve this, you can use the lastIndexOf() method.
String text = "Hello, World!"; int lastIndex = text.lastIndexOf("o");
Behaviour of this:
The replacement proceeds from the beginning of the string to the end. For example, replacing “aa” with “b” in the string “aaa” will result in “ba” rather than “ab”.
What is the behavior of the replace() method in Java?
To acheive this you can use the split() method, which takes a delimiter as a parameter and returns an array of strings.
For example, given the string “Hello, World!”, calling split(“, “) would return an array containing [“Hello”, “World!”].
String text = "Hello, World!"; String[] words = text.split(", ");
How can you split a string into an array of substrings in Java?
How can you extract a substring from a string in Java?
To achieve this you can use the substring() method
String text = "Hello, World!"; String substring = text.substring(7);