Chapter V Flashcards
Working witj Java primitive data types and string APIs Working with Java arrays Programming abstractly through interfaces
What are two ways to create a String object?
String name = "Fluffy"; String name = new String("Fluffy");
What interfaces does String implement?
Serializable, CharSequence, Comparable
What package is String part of?
java.lang
What are the rules for concatenation?
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + means concatenation.
- The expression is evaluated from left to right.
Name as many important String methods as you can (answer contains only methods relevant for OCP exam)
- length()
- charAt()
- indexOf()
- substring()
- toLowerCase() & toUpperCase()
- equals() & equalsIgnoreCase()
- startsWith() & endsWith()
- replace()
- contains()
- trim() & strip() & stripLeading(), and stripTailing()
- intern()
What does the String method length() do?
returns the length of the string, where the first number is 1.
What does the String method charAt() do?
The method charAt() lets you query the string to find out what character is at a specific index.
What does the String method indexOf() do?
The method indexOf() looks at the characters in the string and finds the first index that matches the value.
What does String.indexOf() return?
ex: “test”.indexOf(“z”);
The method returns -1.
What does String.indexOf() return?
ex: “test”.indexOf(“es”);
1
What does String.indexOf() return?
ex: “animals”.indexOf(‘a’);
0.
note: characters can be passed as an argument.
What can be passed as a second argument of the String method indexOf()?
An index that indicates where to start the search.
ex: “animals”.indexOf(‘a’, 4); // returns 4
What will happen when this code is executed?
“test”.charAt(4);
It throws a StringIndexOutOfBoundsException.
What will happen when this code is executed?
“animals”.indexOf(“al”, 5);
It returns -1 (cannot find a match)
What will happen when this code is executed?
“animals”.indexOf(“al”, 5);
It returns -1 (cannot find a match)
What does the String method substring() do?
The method substring() looks for characters in a string and returns part of the string. The first parameter is the index to start with for the returned string.
The String method substring takes two arguments. Is the second argument inclusive in the search?
No.
ex: “animals”.substring(3, 4); // returns m
What will happen when this code is executed?
“animals”.substring(3, 3);
Returns an empty string
What will happen when this code is executed?
“animals”.substring(3, 2);
or
“animals”.substring(3, 8);
Throws an exception.
is the String method equals() case sensitive?
yes
To use equals without case sensitivity, use the method equalsIgnoreCase().
What will happen when this code is executed?
“abc”.equals(“ABC”);
It returns false. Equals is case sensitive.
What will happen when this code is executed?
“abcabc”.replace(‘a’, ‘A’);
It returns “AbcAbc”
What do the strip() and trim() string methods do?
They remove whitespace from the beginning and end of a string.
Whitespace includes \t (tab, \n (newline) and \r (carriage return).
What do the string methods stripLeading() and stripTrailing() do?
stripLeading() removes whitespace from the beginning and stripTrailing() removes whitespace at the end of a string.
Whitespace includes \t (tab, \n (newline) and \r (carriage return).
What does the string method intern() do?
The intern() method returns the value from the string pool if it is there. Otherwise, it adds the value to the string pool.
What is the following technique called?
(bonus: how many objects are created?)
String result = “AniMal “.trim().toLowerCase().replace(‘a’, ‘A’);
The technique is called method chaining.
Bonus answer: 4 objects
Why should we use Stringbuilder instead of String?
Stringbuilder is mutable, whereas Strings are immutable. If we need to work on a piece of String, it might be better to use a StringBuilder to avoid the creation of many objects.
What are the options to construct a StringBuilder?
Stringbuilder b1 = new StringBuilder(); Stringbuilder b2 = new StringBuilder("animal"); Stringbuilder b3 = new StringBuilder(10);
What is the difference between StringBuilder and StringBuffer?
StringBuffer is thread-safe. StringBuilder is faster.
What is an easy way to reverse a String?
Convert it to a StringBuilder and call the reverse() method.
How do we convert a StringBuilder object to a String object?
By calling the toString() method.
Does StringBuilder implement the method equals()?
Why does the following code not compile?
String string = "a"; StringBuilder builder = new StringBuilder("a"); System.out.println(string == builder);
What is the string pool (also known as intern pool)?
What kinds of strings are stored in the string pool?
What will happen when this code is executed?
String x = “Hello world”;
String y = “Hello world”;
System.out.println(x == y);
What will happen when this code is executed?
String x = “Hello world”;
String y = “ Hello world”.trim();
System.out.println(x == y);
How do we force to use a string from a string pool, if it exists?
What are valid ways to declare an array?
What is the following type of array called?
int[] numbers = {42, 55, 99};
What type of reference variable does the following code create?
int[] ids, types;
What type of reference variable does the following code create?
int ids[], types;
How do we print the contents of an array in a human-readable way?
Arrays is a class provided by Java that requires an import. What package does it reside in?
How can we sort an array? Class & method
How can we search an array?
What does Arrays.binarySearch() return when an element is not found?
What does Arrays.binarySearch() return when the argument array is not sorted?
How can we compare an array?
What are the return value rules for Arrays.compare()?
Does this compile?
Arrays.compare(new int[] {1}, new String[] {“a”});
What does Arrays.mismatch() do?
What does the following code return?
Arrays.mismatch(new int[] {1, 2}, new int[] {1});
What is the benefit of an Arraylist over an array?
What package does ArrayList reside in?
What are three ways to instantiate an ArrayList?
Why does
var list = new ArrayList<>();
compile? and what type is it?
What is a wrapper class?
What is the advantage of calling the Wrapper class method valueOf() over using the constructor?
ex: Long.valueOf(1)
Given the following String “123”
Which Wrapper class method can we use to make it into an int and which method can we use to make it into an Integer?
Explain what autoboxing and unboxing is
What happens when you try to unbox a null value?
How do we convert a list to an array? (2 options)
How do we convert an array to a list?
What happens when you want to change a value in an immutable list?
How do we create a list using varargs? (two options)
What is the difference between Arrays.asList() and List.of()?
What is a Set?
What happens when trying to add an item to a set that already contains that item?
What are two common implementations of Set?
When is TreeSet more important than Hashset?
What is a Map?
What is the most common implementation of Map?
What are common Math methods?
What is the difference between the equals method and the == operator?