More on this book
Kindle Notes & Highlights
OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 (Sybex Study Guide)
Started reading
October 14, 2019
boolean startsWith(String prefix) boolean endsWith(String suffix)
boolean contains(String str)
String replace(char oldChar, char newChar) String replace(CharSequence oldChar, CharSequence newChar)
public String trim()
This is very inefficient. Luckily, Java has a solution. The StringBuilder class creates a String without storing all those interim String values. Unlike the String class, StringBuilder is not immutable.
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 number of slots for characters.
StringBuilder append(String str)
you can just call append() without having to convert your parameter to a String first.
StringBuilder delete(int start, int end)
StringBuilder reverse()
String toString()
When writing new code that concatenates a lot of String objects together, you should use StringBuilder.
Since it isn't the same at compile-time, a new String object is created.
The lesson is to never use == to compare String objects. The only time you should have to deal with == for Strings is on the exam.
equals to check the values inside the String rather than the String itself.
If you call equals() on two StringBuilder instances, it will check reference equality.
An array is an area of memory on the heap with space for a designated number of elements.
int[] numbers1 = new int[3];
When using this form to instantiate an array, all elements are set to the default value for that type.
int[] numbers2 = new int[] {42, 55, 99};
size. As a shortcut, Java lets you write this: int[] numbers2 = {42, 55, 99}; This approach is called an anonymous array.
int is a primitive; int[] is an object.
The array does not allocate space for the String objects. Instead, it allocates space for a reference to where the objects are really stored.
The code never instantiated the array so it is just a reference variable to null.
We have a String[] referred to from an Object[] variable. At runtime, the code throws an ArrayStoreException.
length does not consider what is in the array; it only considers how many slots have been allocated.
java.util.Arrays
int[] vars3[]; // 2D array int[] vars4 [], space [][]; // a 2D AND a 3D
Another way to create an asymmetric array is to initialize just an array's first dimension, and define the size of each array component in a separate statement: int [][] args = new
Just like a StringBuilder, ArrayList can change size at runtime as needed.
Like an array, an ArrayList is an ordered sequence that allows duplicates.
ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(10); ArrayList list3 = new ArrayList(list2);
The final example tells Java that we want to make a copy of another ArrayList. We copy both the size and contents of that ArrayList.
ArrayList<String> list4 = new ArrayList<String>(); ArrayList<String> list5 = new ArrayList<>();
ArrayList implements an interface called List. In other words, an ArrayList is a List.
You should also know that ArrayList implements toString()
we didn't specify a type for ArrayList; therefore, the type is Object, which includes everything except primitives.
boolean remove(Object object) E remove(int index)
Since calling remove() with an int uses the index, an index that doesn't exist will throw an exception.
The set() method changes one of the elements of the ArrayList without changing the size. The method signature is as follows: E set(int index, E newElement) The E return type is the element that got replaced.
clear() method provides an easy way to discard all elements of the ArrayList.
boolean contains(Object object)
This method calls equals() on each element of the ArrayList to see whether there are any matches.
ArrayList has a custom implementation of equals() so you can compare two lists to see if they contain the same elements in the same order.
The parse methods, such as parseInt(), return a primitive, and the valueOf() method returns a wrapper class.
int primitive = Integer.parseInt("123"); Integer wrapper = Integer.valueOf(
the exam won't make you recognize that the method parseInt() is used rather than parseInteger().
Character class doesn't participate in the parse/valueOf methods.
Since Java 5, you can just type the primitive value and Java will convert it to the relevant wrapper class for you. This is called autoboxing.