Chapter 3 Core Java APIs Flashcards
What method length() does ?
String string = “animals”;
System.out.println(string.length());
The method length() returns the number of characters in the String. The method signature is as follows: int length()
String string = “animals”;
System.out.println(string.length()); // 7
What method chartAt() does ?
String string = “animals”;
System.out.println(string.charAt(0));
The method charAt() lets you query the string to find out what character is at a specific index. The method signature is as follows: char charAt(int index)
String string = “animals”;
System.out.println(string.charAt(0)); // a
What method indexOf() does ?
String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(“al”, 5));
The method indexOf()looks at the characters in the string and fi nds the fi rst 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. indexOf() returns –1 when no match is found.
String string = “animals”;
System.out.println(string.indexOf(‘a’)); // 0
System.out.println(string.indexOf(“al”)); // 4
System.out.println(string.indexOf(“al”, 5)); // -1
What method substring() does ?
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));
It returns parts of the string. The first parameter is the index to start with for the returned string. 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)); // 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
What toLowerCase() and toUpperCase() do ?
String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toLowerCase());
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.
String string = “animals”;
System.out.println(string.toUpperCase()); // ANIMALS
System.out.println(“Abc123”.toLowerCase()); // abc123
What equals() and equalsIgnoreCase() do ?
System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”));
System.out.println(“abc”.equalsIgnoreCase(“ABC”));
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.
System.out.println(“abc”.equals(“ABC”)); // false
System.out.println(“ABC”.equals(“ABC”)); // true
System.out.println(“abc”.equalsIgnoreCase(“ABC”)); // true
What startsWith() and endsWith() do ?
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));
The startsWith() and endsWith() methods look at whether the provided value matches part of the String.
System.out.println(“abc”.startsWith(“a”)); // true
System.out.println(“abc”.startsWith(“A”)); // false
System.out.println(“abc”.endsWith(“c”)); // true
System.out.println(“abc”.endsWith(“a”)); // false
What method contains() does ?
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));
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.
System.out.println(“abc”.contains(“b”)); // true
System.out.println(“abc”.contains(“B”)); // false
What method replace() does ?
System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));
The replace() method does a simple search and replace on the string. There’s a version that takes char parameters as well as a version that takes CharSequence parameters.
System.out.println(“abcabc”.replace(‘a’, ‘A’)); // AbcAbc
System.out.println(“abcabc”.replace(“a”, “A”)); // AbcAbc
What method trim() does ?
System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());
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.
System.out.println(“abc”.trim()); // abc
System.out.println(“\t a b c\n”.trim()); // a b c
What is the main difference between StringBuilder and StringBuffer ?
When writing new code that concatenates a lot of String objects together, you should use StringBuilder. StringBuilder was added to Java in Java 5. If you come across older code, you will see StringBuffer used for this purpose. StringBuffer does the same thing but more slowly because it is thread safe.
What equals() and ‘==’ do with String / StringBuilder ?
Calling == on String objects will check whether they point to the same object in the
pool. Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object. Calling equals() on String objects will check whether the sequence of characters is the same. Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.
What Arrays.binarySearch() does ?
Arrays.binarySearch() searches a sorted array and returns the
index of a match. If no match is found, it negates the position where the element would need to be inserted and subtracts 1.
What is Autoboxing ?
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
LocalDate d = new LocalDate(); System.out.println(d);
LocalDate d = new LocalDate(); // DOES NOT COMPILE
The date and time classes have private constructors to force you to use the static methods. Don’t fall for this. You are not allowed to construct a date or time object directly.