OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808 (Sybex Study Guide)
Rate it:
Open Preview
37%
Flag icon
boolean startsWith(String prefix) boolean endsWith(String suffix)
37%
Flag icon
boolean contains(String str)
38%
Flag icon
String replace(char oldChar, char newChar) String replace(CharSequence oldChar, CharSequence newChar)
38%
Flag icon
public String trim()
38%
Flag icon
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.
39%
Flag icon
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.
39%
Flag icon
StringBuilder append(String str)
39%
Flag icon
you can just call append() without having to convert your parameter to a String first.
39%
Flag icon
StringBuilder delete(int start, int end)
40%
Flag icon
StringBuilder reverse()
40%
Flag icon
String toString()
40%
Flag icon
When writing new code that concatenates a lot of String objects together, you should use StringBuilder.
40%
Flag icon
Since it isn't the same at compile-time, a new String object is created.
40%
Flag icon
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.
40%
Flag icon
equals to check the values inside the String rather than the String itself.
40%
Flag icon
If you call equals() on two StringBuilder instances, it will check reference equality.
40%
Flag icon
An array is an area of memory on the heap with space for a designated number of elements.
41%
Flag icon
int[] numbers1 = new int[3];
41%
Flag icon
When using this form to instantiate an array, all elements are set to the default value for that type.
41%
Flag icon
int[] numbers2 = new int[] {42, 55, 99};
41%
Flag icon
size. As a shortcut, Java lets you write this: int[] numbers2 = {42, 55, 99}; This approach is called an anonymous array.
41%
Flag icon
int is a primitive; int[] is an object.
41%
Flag icon
The array does not allocate space for the String objects. Instead, it allocates space for a reference to where the objects are really stored.
41%
Flag icon
The code never instantiated the array so it is just a reference variable to null.
41%
Flag icon
We have a String[] referred to from an Object[] variable. At runtime, the code throws an ArrayStoreException.
42%
Flag icon
length does not consider what is in the array; it only considers how many slots have been allocated.
42%
Flag icon
java.util.Arrays
42%
Flag icon
For the OCP exam, you'll learn how to create custom sort orders using something called a comparator.
Prashant Kumar Rai
Exam Alert
42%
Flag icon
int[] vars3[];          // 2D array int[] vars4 [], space [][];  // a 2D AND a 3D
43%
Flag icon
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
43%
Flag icon
Just like a StringBuilder, ArrayList can change size at runtime as needed.
43%
Flag icon
Like an array, an ArrayList is an ordered sequence that allows duplicates.
43%
Flag icon
ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(10); ArrayList list3 = new ArrayList(list2);
43%
Flag icon
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.
43%
Flag icon
ArrayList<String> list4 = new ArrayList<String>(); ArrayList<String> list5 = new ArrayList<>();
43%
Flag icon
ArrayList implements an interface called List. In other words, an ArrayList is a List.
43%
Flag icon
You should also know that ArrayList implements toString()
44%
Flag icon
we didn't specify a type for ArrayList; therefore, the type is Object, which includes everything except primitives.
44%
Flag icon
boolean remove(Object object) E remove(int index)
44%
Flag icon
Since calling remove() with an int uses the index, an index that doesn't exist will throw an exception.
44%
Flag icon
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.
44%
Flag icon
clear() method provides an easy way to discard all elements of the ArrayList.
44%
Flag icon
boolean contains(Object object)
44%
Flag icon
This method calls equals() on each element of the ArrayList to see whether there are any matches.
44%
Flag icon
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.
45%
Flag icon
The parse methods, such as parseInt(), return a primitive, and the valueOf() method returns a wrapper class.
45%
Flag icon
int primitive = Integer.parseInt("123"); Integer wrapper = Integer.valueOf(
45%
Flag icon
the exam won't make you recognize that the method parseInt() is used rather than parseInteger().
45%
Flag icon
Character class doesn't participate in the parse/valueOf methods.
45%
Flag icon
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.