Chapter 5 Core Java APIs Flashcards
What is API?
Application Programming Interface (API),
an interface refers to a group of classes or Java interface definitions giving you access to a service or functionality.
Creating and Manipulating Strings
What is a string?
A string is basically a sequence of characters;
here’s an example:
String name = "Fluffy";
the String class is special and doesn’t need to be instantiated with new.
String name = "Fluffy";
VS
String name = new String("Fluffy");
the String class is special and doesn’t need to be instantiated with new.
Since a String is a sequence of characters, you probably won’t be surprised to hear that it implements the interface CharSequence.
This interface is a general way of representing several classes, including String and StringBuilder. You’ll learn more about interfaces later in the book.
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"); System.out.println("c" + 1 + 2);
System.out.println(1 + 2); // 3 System.out.println("a" + "b"); // ab System.out.println("a" + "b" + 3); // ab3 System.out.println(1 + 2 + "c"); // 3c System.out.println("c" + 1 + 2); // c12
int three = 3; String four = "4"; System.out.println(1 + 2 + three + four);
int three = 3; String four = "4"; System.out.println(1 + 2 + three + four);//64
4: String s = "1"; 5: s += "2"; 6: s += 3; 7: System.out.println(s);
4: String s = "1"; // s currently holds "1" 5: s += "2"; // s currently holds "12" 6: s += 3; // s currently holds "123" 7: System.out.println(s); // 123
String is immutable
Once a String object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change one of the characters inside it.
MORE ON IMMUTABILITY
Immutable has only a getter. There’s no way to change the value of s once it’s set. Mutable has a setter. This allows the reference s to change to point to a different String later. Note that even though the String class is immutable, it can still be used in a mutable class. You can even make the instance variable final so the compiler reminds you if you accidentally change s.
Also, immutable classes in Java are final, which prevents subclasses creation. You wouldn’t want a subclass adding mutable behavior.
class Mutable { private String s; public void setS(String newS){ s = newS; } // Setter makes it mutable public String getS() { return s; } } final class Immutable { private String s = "name"; public String getS() { return s; } }
IMPORTANT STRING METHODS
- length()
- charAt()
- indexOf()
- substring()
- toLowerCase() and toUpperCase()
- equals() and equalsIgnoreCase()
- startsWith() and endsWith()
- replace()
- contains()
- trim(), strip(), stripLeading(), and stripTrailing()
- intern()
int length()
The method length() returns the number of characters in the String.
char charAt(int index)
The method charAt() lets you query the string to find out what character is at a specific index.
String string = "animals"; System.out.println(string.charAt(0)); System.out.println(string.charAt(6)); System.out.println(string.charAt(7));
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 java.lang.StringIndexOutOfBoundsException: String index out of range: 7
int indexOf(int ch) int indexOf(int ch, int fromIndex) int indexOf(String str) int indexOf(String str, int fromIndex)
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.
Remember that a char can be passed to an int parameter type.
On the exam, you’ll only see a char passed to the parameters named ch.
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. Because indexes start with 0, the caller knows that –1 couldn’t be a valid index. This makes it a common value for a method to signify to the caller that no match is found.
indexOf() examples:
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));
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 substring(int beginIndex) String substring(int beginIndex, int endIndex)
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.
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));
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)); System.out.println(string.substring(3, 2)); System.out.println(string.substring(3, 8));
System.out.println(string.substring(3, 3)); // empty string System.out.println(string.substring(3, 2)); // Exception java.lang.StringIndexOutOfBoundsException: begin 3, end 2, length 7 System.out.println(string.substring(3, 8)); // Exception java.lang.StringIndexOutOfBoundsException: begin 3, end 8, length 7
String toLowerCase() String toUpperCase()
toUpperCase() converts any lowercase characters to uppercase in the returned string.
toLowerCase() converts any uppercase characters to lowercase in the returned string.
These methods leave alone any characters other than letters.
Also, remember that strings are immutable, so the original string stays the same.
toUpperCase() and toLowerCase() examples:
String string = "animals"; System.out.println(string.toUpperCase()); System.out.println("Abc123".toLowerCase());
String string = "animals"; System.out.println(string.toUpperCase()); // ANIMALS System.out.println("Abc123".toLowerCase()); // abc123
boolean equals(Object obj) boolean equalsIgnoreCase(String str)
The equals() method checks whether two String objects contain exactly the same characters 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.
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.
equals() and equalsIgnoreCase() examples
System.out.println("abc".equals("ABC")); System.out.println("ABC".equals("ABC")); System.out.println("abc".equalsIgnoreCase("ABC"));
System.out.println("abc".equals("ABC")); // false System.out.println("ABC".equals("ABC")); // true System.out.println("abc".equalsIgnoreCase("ABC")); // true