Chapter 3 Flashcards
String concatenation
- 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
Mutable vs. Immutable
Mutable is another word for changeable. Immutable is the opposite—an object that can’t be changed once it’s created. On the OCA exam, you need to know that String is immutable.
The string pool
Also known as the intern pool, is a location in the Java virtual machine (JVM) that collects all these strings. Java realizes that many strings repeat in the program and solves this issue by reusing common ones.
Important String Methods:
length()
String string = “animals”;
System.out.println(string.length());
// 7
The method length() returns the number of characters in the String.
Important String Methods:
charAt()
String string = “animals”;
System.out.println(string.charAt(0));
System.out.println(string.charAt(6));
System.out.println(string.charAt(7));
// a
// s
// throws exception
The method charAt() lets you query the string to find out what character is at a specific index.
Important String Methods:
indexOf()
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));
// 0
// 4
// 4
// -1
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)
Unlike charAt(), the indexOf() method doesn’t throw an exception if it can’t find a match. indexOf() returns –1 when no match is found.
Important String Methods:
substring()
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, 7));
System.out.println(string.substring(3, 3));
System.out.println(string.substring(3, 2));
System.out.println(string.substring(3, 8));
// mals
// mals
// m
// mals
// empty string
// throws exception
// throws exception
The method substring() also looks for characters in a string. It returns parts of the string. The first 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.” The method signatures are as follows:
int substring(int beginIndex)
int substring(int beginIndex, int endIndex)
Let’s review this one more time since substring() is so tricky. 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.
Important String Methods:
equals() and equalsIgnoreCase()
System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”)); System.out.println(“abc”.equalsIgnoreCase(“ABC”));
// false
// true
// true
The equals() method checks whether two String objects contain exactly the same char- acters in the same order. The equalsIgnoreCase() method checks whether two String objects contain the same characters with the exception that it will convert the characters’ case if needed. The method signatures are as follows:
boolean equals(String str)
boolean equalsIgnoreCase(String str)
Important String Methods:
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
The startsWith() and endsWith() methods look at whether the provided value matches part of the String. The method signatures are as follows:
boolean startsWith(String prefix)
boolean endsWith(String suffix)
Again, nothing surprising here. Java is doing a case-sensitive check on the values provided.
Important String Methods:
contains()
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));
// true
// false
The contains() method also looks for matches in the String. It isn’t as particular as startsWith() and endsWith()—the match can be anywhere in the String. The method signature is as follows:
boolean contains(String str)
Again, we have a case-sensitive search in the String. The contains() method is a conve- nience method so you don’t have to write str.indexOf(otherString) != -1.
Important String Methods:
replace()
System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));
// AbcAbc
// AbcAbc
The replace() method does a simple search and replace on the string. The method signatures are as follows:
String replace(char oldChar, char newChar)
String replace(CharSequence oldChar, CharSequence newChar)
The first example uses the first method signature, passing in char parameters. The second example uses the second method signature, passing in String parameters.
Important String Methods:
trim()
System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());
// abc
// a b c
The trim() method removes whitespace from the beginning and end of a String. In terms of the exam, whitespace consists of spaces along with the \t (tab) and \n (newline) characters. Other characters, such as \r (carriage return), are also included in what gets trimmed. The method signature is as follows:
public String trim()
The StringBuilder class
The StringBuilder class creates a String without storing all those interim String values. Unlike the String class, StringBuilder is not immutable.
Creating a StringBuilder
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder(“animal”);
StringBuilder sb3 = new StringBuilder(10);