Chapter 3 Flashcards
What does API stand for?
application programming interfaces that give you access to a service or functionality
What is string concatenation?
Placing one String before another String and combining them
what does the + mean here: “a” + “b”
concatenation
What is the order of evaluation with addition and concatenation?
evaluated left to right
What is the output of the following: sys out(1+3); sys out(“a”+”b”); sys out(“a”+”b”+3; sys out(1+2+”c”);
4 ab ab3 3c
What is the output and type of the sysout: int three = 3; String four = “4”; sysout(1+2+three+four):
string 64
What does += do in the context of Strings? (What does s+=”2 mean?)
s = s + “2”;
What is the output? String s = “1”; s +=”2”; s +=”3”; sysout(s)
s is equal to “123”;
What doe sit mean to say that Strings are immutable?
They are not allowed to change. It cannot be made larger or smaller and you can’t change one of the letters inside of it. Nothing about it can change!
What is the output? String s1 = “1”; String s2 = s1.concat(“2”); s2.concat(“3”); System.out.println(s2);
- s2.concat 3 would need to be stored in a new variable
What is the output? String s1 = “1”; String s2 = s1.concat(“2”); s2.concat(“3”); System.out.println(s2);
- s2.concat 3 would need to be stored in a new variable
What does Java do because it realizes Strings take up a lot of memory?
It is able to recognize when one String is use multiple times and reuses common ones.
What is the String pool?
The place containing literal values that appear in your program. For example: “hello” would be in the string pool whereas myObject.toString() would not be in the String pool.
Would “hello” be in the string pool
yes
would myOjbect.toString() be in the string pool?
no
How does the string pool handle this? String name = “fluffy”; String name = new String(“fluffy”);
The first is stored in the string pool. The second says, “No, JVM. I really don’t want you to use the string pool for this one. Make a new object for me even though it is less efficient.
Why does the string pool exist?
Because strings take up a lot of memory. So the pool stores literals and watches for ways to reuse preexisting literals where possible.
With Strings, where does Java index from?
0 (only when indexing
What does myString.length() return?
an int count of the length of myString
What’s the method signature for length()
int length()
What’s the method signature for charAt()
char charAt(int index)
What does charAt(int index) return?
The char at a specified index
What does this return? String string = “animals”; sysout(charAt(0)); sysout(charAt(6)); sysout(charAt(7));
a s throws out of bounds exception because charAt indexes from 0. So max range would be 0-6.
What does indexOf() do?
indexOf() looks at the characters in a string and finds the earliest occurrence, returning the index of the first occurrence as an int.
What do these return? String string = “animals”; sysout(string.indexOf(‘a’));
sysout(string.indexOf(“al”));
sysout(string.indexOf(‘a’,4));
sysout(string.indexOf(“al”,5));
0
4
4 (don’t even look for any matches until you get to the index of 4)
-1 (negative one means no matches were found) Remember that you need double quotes for more than one letter
Does indexOf throw an error if it doesn’t find a match?
No. It will return -1 if it doesn’t find anything.
What are the method signatures for indexOf()? (4)
int indexOf(int ch); int indexOf(char ch, int fromIndex); int indexOf(String str); int indexOf(String str, index fromIndex);
What are the method signatures for substring()?
String subString(int beginIndex); String subString(int beginIndex, int endIndex);
What does substring() do?
The method returns the string starting from the requested index. If an end index is requested, it stops right before that index. Otherwise, it goes to the end of the string.
What does this return? String string = “animals”; System.out.println(string.substring(3)); System.out.println(string.substring(string.indexOf(‘m’))); System.out.println(string.substring(3, 4)); System.out.println(string.substring(3, 6)); System.out.println(string.substring(3, 8)); System.out.println(string.substring(3,3)); System.out.println(string.substring(3,2));
mals mals m mal error empty string error Remember, the last index is not INCLUDING. It’s says, “include up to this index”. If an endIndex is requested it stops right there.
What do String toLowerCase() and String toUpperCase() do?
Keeping in mind that strings are immutable, toUpperCase and toLowerCase return strings that are either now upper or lower case. They only touch characters that are letters.
What does boolean equals(Object obj) do?
It compares whether two objects have the exact same bits. You can only call this on objects. Not primitives.
What does boolean equalsIgnoreCase(Object obj) do?
Checks whether two String objects contains exactly the same letters in the same order with the exception that it will convert case when it needs to)
What’s the output? String string = “animals”; String string2 = new String(“animals”); String string3 = “animals”; System.out.println(string.equals(“animals”)); System.out.println(string.equals(string2)); System.out.println(string.equalsIgnoreCase(new String(“animals”))); System.out.println(string==string2); System.out.println(string==”animals”); System.out.println(string==new String(“animals”)); System.out.println(string==string3);
true true true false true false true remember it is case sensitive unless told to not be so
What do these methods do? boolean startsWith(String prefix) boolean endsWith(String suffix)
They look at whether or not the string ends with or starts with the provided values
What’s the output? String name = “jedd”; System.out.println(name.endsWith(“dd”)); System.out.println(name.endsWith(“DD”));
true false remember it is case sensitive
What does boolean contains (String str) do?
It looks for matches with in the string regardless of whether it starts with/ends with or whatever. it doesn’t care. also case sensitive
What is the output? String name = “jedd”; System.out.println(name.contains(“dd”)); System.out.println(name.contains(“DD”));
true false
What does the replace method do? String replace(char oldChar, char newChar) String replace(CharSequence oldChar, CharSequence newChar)
The replace method does a simple search and replace on the string.
What is the output: System.out.println(“abcabcabc”.replace(‘c’, ‘b’)); System.out.println(“abcabcabc”.replace(“a”, “A”));
abbabbabb AbcAbcAbc Note: the second method is not the same as the first. This is looking for a CharSequence or a String and replacing it with another String.
What does the trim() method do? public String trim()
The trim() method removes whitespace from the beginning and the end of a string. So on the exam this means \t (tab and \n newline characters get trimmed.
What is the output? public static void main(String[] args) { System.out.println(“abc”.trim()); System.out.println(“\t a b c\n”.trim()
abc a b c The second drops the leading tab, subsequent spaces, and the trailing newline. Note that it leaves the spaces in the middle of the spring.
What happens when you call a String method on a String? What happens if you call many methods on a String?
You make a new String each time. Each time one is called, the returned value is put in a new variable.
What does the Stringbuilder class do?
Creates a String without storing all the interim values that occur as you iterate through string objects. String immutability means that each time you “change” a String you are actually making a new one. Often making the old one eligible for garbage collection almost immediately. String builders change their own state and then return references to themselves.
Are instances of StringBuilder s immutable?
no
What is the output and why? StringBuilder a = new StringBuilder(“abc”); StringBuilder b = a.append(“de”); b = b.append(“f”).append(“g”); System.out.println(a); System.out.println(b); System.out.println(a==b);
abcdefg abcdefg true There is only one StringBuilder object here. We know that because new StringBuilder9) was called only once.
How many string objects does this piece of code create? String alpha = “”; for(char current = ‘a’; current <= ‘z’; current++){ alpha+=current; System.out.println(alpha);
- A new string is made with the addition of each new character. (initial empty string + addition of each character of the alphabet). Note that as each string is created the string that it replaced is now available for garbage collection.
How many string objects does this code generate? StringBuilder alpha = new StringBuilder(); for (char current = ‘a’; current <= ‘z’; current++) { alpha.append(current); System.out.println(alpha); }
Just one. This code reuses the same String building without creating an interim string each time.
How does String handle method chaining?
It creates a new string each time.
How does StringBuilder handle method chaining?
StringBuilders change their own state and returns a reference to itself.
What is the output? StringBuilder sb = new StringBuilder(); sb.append(“+middle”); StringBuilder same = sb.append(“+end”); System.out.println(sb); System.out.println(sb == same);
+middle+end true
What’s the difference between StringBuilder() and StringBuffer()?
String Buffer is an older thread safe version of string builder.