Programmer I Chapter 5: Core Java APIs Flashcards
what will this print?
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);
64
what will this print?
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
System.out.println(s2);
12
what will this print?
String string = “animals”;
System.out.println(string.charAt(0));
System.out.println(string.charAt(6));
System.out.println(string.charAt(7));
a
s
3rd line throws an exception
what will this print?
String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(‘a’, 4));
System.out.println(string.indexOf(“al”, 5)); // -1
0
4
4
-1
what will this print?
4: StringBuilder a = new StringBuilder(“abc”);
5: StringBuilder b = a.append(“de”);
6: b = b.append(“f”).append(“g”);
7: System.out.println(“a=” + a);
8: System.out.println(“b=” + b);
abcdefg
abcdefg
what will this print?
3: StringBuilder sb = new StringBuilder(“animals”);
4: sb.insert(7, “-“);
5: sb.insert(0, “-“);
6: sb.insert(4, “-“);
7: System.out.println(sb);
-ani-mals-
what is the value of sb?
StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1, 100);
a
The delete() method is more flexible than some others when it comesto array indexes. If you specify a second parameter that is past the endof the StringBuilder, Java will just assume you meant the end
what will this print?
1: public class Tiger {
2: String name;
3: public static void main(String[] args) {
4: Tiger t1 = new Tiger();
5: Tiger t2 = new Tiger();
6: Tiger t3 = t1;
7: System.out.println(t1 == t3);
8: System.out.println(t1 == t2);
9: System.out.println(t1.equals(t2));
10: } }
true
false
false
3rd is false because Tiger does not implement equals()
what will this print?
String string = "a"; StringBuilder builder = new StringBuilder("a"); System.out.println(string == builder);
won’t compile.
Remember that == is checking for object reference equality. Thecompiler is smart enough to know that two references can’t possiblypoint to the same object when they are completely different types.
What is the string pool?
The string pool contains literal values and constants that appear inyour program. For example, “name” is a literal and therefore goes intothe string pool. myObject.toString() is a string but not a literal, so itdoes not go into the string pool.
What will this print?
String x = “Hello World”;
String y = “Hello World”;
System.out.println(x == y);
true
the string literal comes from the string pool
What will this print?
String x = “Hello World”;
String z = “ Hello World”.trim();
System.out.println(x == z);
false
In this example, we don’t have two of the same String literal. Althoughx and z happen to evaluate to the same string, one is computed atruntime.
What will this print?
String name = "Hello World"; String name2 = new String("Hello World").intern(); System.out.println(name == name2);
true
First we tell Java to use the string pool normally for name. Then forname2, we tell Java to create a new object using the constructor but tointern it and use the string pool anyway. Since both variables point tothe same reference in the string pool, we can use the == operator.
what will this print?
15: String first = “rat” + 1;
16: String second = “r” + “a” + “t” + “1”;
17: String third = “r” + “a” + “t” + new String(“1”);
18: System.out.println(first == second);
19: System.out.println(first == second.intern());
20: System.out.println(first == third);
21: System.out.println(first == third.intern());
true true false true
On line 15, we have a compile-time constant that automatically gets placed in the string pool as “rat1”. On line 16, we have a more complicated expression that is also a compile-time constant. Therefore, first and second share the same string pool reference. This makes line 18 and 19 print true. On line 17, we have a String constructor. This means we no longer have a compile-time constant, and third does not point to a reference in the string pool. Therefore, line 20 prints false. On line 21, the intern() call looks in the string pool. Java notices that first points to the same String and prints true.
what is the result of this code?
int[] ids, types;
two variables of type int[] are declared
what is the result of this code?
int ids[], types;
two variables are declared. ids has type int[] and types has type int.
All we did was move the brackets, but it changed the behavior. Thistime we get one variable of type int[] and one variable of type int.Java sees this line of code and thinks something like this: “Theywant two variables of type int. The first one is called ids[]. Thisone is an int[] called ids. The second one is just called types. Nobrackets, so it is a regular integer.”
what this array points to?
class Names { String names[]; }
names is null, b.c. instance variable is initialized with the default value and arrays are objects.
what this array points to?
class Names { String names[] = new String[2]; }
It is an array because it has brackets. It is an array of type String since that is the type mentioned in the declaration. It has two elements because the length is 2. Each of those two slots currently is null but has the potential to point to a String object.
what happens at lines ?
3: String[] strings = { “stringValue” };
4: Object[] objects = strings;
5: String[] againStrings = (String[]) objects;
6: againStrings[0] = new StringBuilder();
7: objects[0] = new StringBuilder();
line 6 contains a compilation error;
line 7 throws an exception at runtime
Line 3 creates an array of type String. Line 4 doesn’t require a castbecause Object is a broader type than String. On line 5, a cast isneeded because we are moving to a more specific type. Line 6 doesn’tcompile because a String[] only allows String objects andStringBuilder is not a String.
Line 7 is where this gets interesting. From the point of view of thecompiler, this is just fine. A StringBuilder object can clearly go in anObject[]. The problem is that we don’t actually have an Object[]. Wehave a String[] referred to from an Object[] variable. At runtime, thecode throws an ArrayStoreException. You don’t need to memorize thename of this exception, but you do need to know that the code willthrow an exception.
How does Arrays.compare(arr1, arr2) work?
- meaning of return value
- how are arrays of different langht compared
- how are each values compared
First you need to learn what the return value means. You do not need to know the exact return values, but you do need to know the following:
- A negative number means the first array is smaller than the second.
- A zero means the arrays are equal.
- A positive number means the first array is larger than the second.
Now let’s look at how to compare arrays of different lengths:
- If both arrays are the same length and have the same values in each spot in the same order, return zero.
- If all the elements are the same but the second array has extra elements at the end, return a negative number.
- If all the elements are the same but the first array has extra elements at the end, return a positive number.
- If the first element that differs is smaller in the first array, return a negative number.
- If the first element that differs is larger in the first array, return a positive number.
Finally, what does smaller mean? Here are some more rules that apply here and to compareTo():
- null is smaller than any other value.
- For numbers, normal numeric order applies.
- For strings, one is smaller if it is a prefix of another.
- For strings/characters, numbers are smaller than letters.
- For strings/characters, uppercase is smaller than lowercase