Chapter 3 Flashcards
(130 cards)
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.