Chapter 3 Core Java APIs Flashcards
Using operators and decision constructs Creating and using arrays Working with selected classes from the Java API Working with Java Data Types
API stands for
application programming interface.
string is basically a sequence of [Blank]
characters
reference types are created using the [Blank] keyword
new
3 string concatenation rules:
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + means concatenation.
- The expression is evaluated left to right.
System.out.println(1 + 2);
System.out.println(“a” + “b”);
System.out.println(“a” + “b” + 3);
System.out.println(1 + 2 + “c”);
3
ab
ab3
3c
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);
1 + 2 = 3
3 + three = 6
6 + four = 64
String s = “1”;
s += “2”;
s += 3;
System.out.println(s);
123
Can a String be changed when it is created?
No, String is immutable
[Blank] means changeable (as in StringBuffers/StringBuilders)
Mutable
Immutable classes in Java are [Blank], and subclasses can’t add mutable behavior.
final
String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
System.out.println(s2);
12
What location in the JVM collects Strings to reuse and save memory?
String Pool, also known as the intern pool
Which one uses the String pool? String name = "Fluffy"; String name = new String("Fluffy");
First one uses String pool.
Second one creates a new object
Java counts from [Blank] when indexed
0
String class method: length()
The method length() returns the number of characters in the String. The method signature is as follows:
String string = “animals”;
System.out.println(string.length()); // 7
String class method: charAt()
The method charAt() lets you query the string to fi nd out what character is at a specifi c
index. The method signature is as follows:
String string = “animals”;
System.out.println(string.charAt(0)); // a
System.out.println(string.charAt(6)); // s
System.out.println(string.charAt(7)); // throws exception
String class method: indexOf()
The method indexOf()looks at the characters in the string and finds the first index that matches the desired value. indexOf can work with an individual character or a whole String as input. It can also start from a requested position. The method signatures are as follows:
int indexOf(char ch) int indexOf(char ch, index fromIndex) int indexOf(String str) int indexOf(String str, index fromIndex)
String string = “animals”;
System.out.println(string.indexOf(‘a’)); // 0
System.out.println(string.indexOf(“al”)); // 4
System.out.println(string.indexOf(‘a’, 4)); // 4
System.out.println(string.indexOf(“al”, 5)); // -1
String class method: substring()
The method substring() also looks for characters in a string. It returns parts of the string.
The fi rst parameter is the index to start with for the returned string. As usual, this is a
zero-based index. There is an optional second parameter, which is the end index you want
to stop at.
Notice we said “stop at” rather than “include.” This means the endIndex parameter is
allowed to be 1 past the end of the sequence if you want to stop at the end of the sequence.
That would be redundant, though, since you could omit the second parameter entirely in
that case. In your own code, you want to avoid this redundancy. Don’t be surprised if the
exam uses it though. The method signatures are as follows:
int substring(int beginIndex)
int substring(int beginIndex, int endIndex)
String string = “animals”;
System.out.println(string.substring(3)); // mals
System.out.println(string.substring(string.indexOf(‘m’))); // mals
System.out.println(string.substring(3, 4)); // m
System.out.println(string.substring(3, 7)); // mals
String class method: toLowerCase() and toUpperCase() String string = "animals"; System.out.println(string.toUpperCase()); System.out.println("Abc123".toLowerCase());
ANIMALS
abc123
String class method: equals() and equalsIgnoreCase() System.out.println("abc".equals("ABC")); System.out.println("ABC".equals("ABC")); System.out.println("abc".equalsIgnoreCase("ABC"));
false
true
true
String class method: startsWith() and endsWith() System.out.println("abc".startsWith("a")); System.out.println("abc".startsWith("A")); System.out.println("abc".endsWith("c")); System.out.println("abc".endsWith("a"));
true
false
true
false
String class method: contains() System.out.println("abc".contains("b")); // true System.out.println("abc".contains("B")); // false
true
false