Chapter 5 Core Java APIs Notes Flashcards
+ operator rules:
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + means concatenation.
- The expression is evaluated left to right.
IMPORTANT STRING METHODS
-
int length()
returns the number of characters in the String. -
char charAt(int index)
query the string to find out what character is at a specific index. -
int indexOf(int ch)
int indexOf(int ch, int fromIndex)
int indexOf(String str)
int indexOf(String str, int fromIndex)
looks at the characters in the string and finds the first index that matches the desired value.
Remember that a char can be passed to an int parameter type.indexOf()
returns –1 when no match is found. -
String substring(int beginIndex)
String substring(int beginIndex, int endIndex)
It returns parts of the string. -
String toLowerCase()
String toUpperCase()
-
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
equals()
takes an Object rather than a String. This is because the method is the same for all objects. If you pass in something that isn’t a String, it will just return false. -
boolean startsWith(String prefix)
boolean endsWith(String suffix)
-
String replace(char oldChar, char newChar)
String replace(CharSequence target, CharSequence replacement)
does a simple search and replace on the string. -
boolean contains(CharSequence charSeq)
looks for matches in the String. -
String strip()
String stripLeading()
String stripTrailing()
String trim()
The strip() method is new in Java 11. It does everything that
trim() does, but it supports Unicode. -
String intern()
returns the value from the string pool if it is there. Otherwise, it adds the value to the string pool.
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 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 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 System.out.println(string.substring(3, 3)); // empty string System.out.println(string.substring(3, 2)); // throws exception System.out.println(string.substring(3, 8)); // throws exception
System.out.println("abc".strip()); // abc System.out.println("\t a b c\n".strip()); // a b c String text = " abc\t "; System.out.println(text.trim().length()); // 3 System.out.println(text.strip().length()); // 3 System.out.println(text.stripLeading().length()); // 4 System.out.println(text.stripTrailing().length());// 4
First, remember that \t is a single character. The backslash escapes the t to represent a tab.
> [!Note:]
You don’t need to know about Unicode for the exam. But if you want to test the difference, one of Unicode whitespace characters is as follows:
Unicode whitespace characters is as follows:
char ch = '\u2000';
10: String alpha = ""; 11: for(char current = 'a'; current <= 'z'; current++) 12: alpha += current; 13: System.out.println(alpha); 15: StringBuilder alpha = new StringBuilder(); 16: for(char current = 'a'; current <= 'z'; current++) 17: alpha.append(current); 18: System.out.println(alpha);
abcdefghijklmnopqrstuvwxyz
4: StringBuilder sb = new StringBuilder("start"); 5: sb.append("+middle"); // sb = "start+middle" 6: StringBuilder same = sb.append("+end"); // "start+middle+end"
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);
printed:
a=abcdefg b=abcdefg
There are three ways to construct a StringBuilder:
StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("animal"); StringBuilder sb3 = new StringBuilder(10);
- The first says to create a StringBuilder containing an empty sequence of characters and assign sb1 to point to it.
- The second says to create a StringBuilder containing a specific value and assign sb2 to point to it.
- For the first two, it tells Java to manage the implementation details.
- The final example tells Java that we have some idea of how big the eventual value will be and would like the StringBuilder to reserve a certain capacity, or number of slots, for characters.
IMPORTANT STRINGBUILDER METHODS
-
charAt()
, work exactly the same as in the String class. -
indexOf()
, work exactly the same as in the String class. -
length()
, work exactly the same as in the String class. -
substring()
, work exactly the same as in the String class. -
StringBuilder append(String str)
It adds the parameter to the StringBuilder and returns a reference to the current StringBuilder. -
StringBuilder insert(int offset, String str)
adds characters to the StringBuilder at the requested index and returns a reference to the current StringBuilder. -
StringBuilder delete(int startIndex, int endIndex)
StringBuilder deleteCharAt(int index)
StringBuilder replace(int startIndex, int endIndex, String newString)
StringBuilder reverse()
String toString()
3: StringBuilder sb = new StringBuilder("animals"); 4: sb.insert(7, "-"); // sb = animals- 5: sb.insert(0, "-"); // sb = -animals- 6: sb.insert(4, "-"); // sb = -ani-mals- 7: System.out.println(sb); jshell> StringBuilder sb = new StringBuilder("animals"); jshell> sb.insert(8, "-"); | Exception java.lang.StringIndexOutOfBoundsException: offset 8, length 7 | at String.checkOffset (String.java:4576) | at AbstractStringBuilder.insert (AbstractStringBuilder.java:1170) | at StringBuilder.insert (StringBuilder.java:336) | at (#11:1)
StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1, 3); // sb = adef sb.deleteCharAt(5); // throws an exception StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1, 100); // sb = a
StringBuilder builder = new StringBuilder("pigeon dirty"); builder.replace(3, 6, "sty"); System.out.println(builder); // pigsty dirty
StringBuilder builder = new StringBuilder("pigeon dirty"); builder.replace(3, 100, ""); System.out.println(builder);
prints: pig
StringBuilder did not implement equals().
You can call toString() on StringBuilder to get a String to check for equality instead.
THE STRING POOL
The string pool, also known as the intern pool, is a location in the Java virtual machine (JVM) that collects all these strings.
String x = "Hello World"; String y = "Hello World"; System.out.println(x == y); // true
String x = "Hello World"; String z = " Hello World".trim(); System.out.println(x == z); // false
Since it isn’t the same at compile-time, a new String object is created.
String singleString = "hello world"; String concat = "hello "; concat += "world"; System.out.println(singleString == concat);
This prints false. Concatenation is just like calling a method and results in a new String.
String x = "Hello World"; String y = new String("Hello World"); //create a new String System.out.println(x == y); // false
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 for name2, we tell Java to create a new object using the constructor but to intern it and use the string pool anyway.
- Since both variables point to the same reference in the string pool, we can use the == operator.
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); // true 19: System.out.println(first == second.intern()); // true 20: System.out.println(first == third); // false 21: System.out.println(first == third.intern()); // 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.