Chapter 3 Flashcards
System.out.println(1 + 2 + “c”); ?
3c
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
System.out.println(s2);
?
12
String pool or intern pool is what?
A location in the JVM that collects reusable strings.
Which is less efficient. Why? String name = "Fluffy"; String name = new String("Fluffy");
new keyword is less efficient as string pool is not used. Here fluffy is not a literal.
How do these string methods work? length() charAt() indexOf() substring()
string. length();
string. charAt(5);
string. indexOf(char, index);
string. substring(begin, end);
string. substring(begin);
write these string methods:
startsWith()
endsWith
equals
“abc”.startsWith(“a”) // true
“Abc”.endsWith(“c”) // true
“Abc”.equals(“abc”) // false
String methods:
replace()
contains()
“abcabc”.replace(‘a’, ‘A’);
“abcabc”.replace(“a”, “A”);
“Abc”.contains(“b”);
Stringbuilder, immutable or mutable? And String?
Mutable, i.e. Changeable.
String however is immutable, unchangeable.
Logical equality vs object equality for strings?
== is object/reference equality
.equals is logical equality
Strings have special equals method, usually .equals checks reference equality.
Which is which? parseBoolean(“true”) valueOf(“TRUE”)
First converts string to primitive. Second string to wrapper class.
Which wrapper class constructors need casts?
new Byte((byte) 1) new Short((short) 1)
What is a fixed size list or backed list?
An array changed into a basic List (not an ArrayList). Using set() on this List updates both array and list.
List list = Arrays.asList(array)
Sort an array?
Sort an ArrayList?
Arrays.sort(array);
Collections.sort(arraylist);
What are the three time classes? Do they have constructors?
LocalTime, LocalDate, LocalDateTime.
No, example:
LocalTime time = LocalTime.of(6,15);
Is LocalDate mutable or immutable?
Immutable. Therefore must assign to reference when manipulating it.