Chapter 3 - Core Java APIs Flashcards
String Concatenation
(3)
- If both operands are numeric, + means numeric addition.
- f either operand is a String, + means concatenation.
- The expression is evaluated left to right.
(String) Immutability
(1)
Once a String object is created, it is not allowed to change. It cannot be made larger or smaller, and you cannot change characters inside it.
The String Pool
(2)
- The string pool, also known as the intern pool, is a location in the Java virtual machine (JVM) that collects all these strings.
- The string pool contains literal values that appear in your program.
String Methods
(12)
- int length()
- char charAt(int index)
- indexOf
- int indexOf(char ch)
- int indexOf(char ch, index fromIndex)
- int indexOf(String str)
- int indexOf(String str, index fromIndex)
- substring
- String substring(int beginIndex)
- String substring(int beginIndex, int endIndex)
- String toLowerCase(String str)
- String toUpperCase(String str)
- equals
- boolean equals(String str)
- boolean equalsIgnoreCase(String str)
- boolean startsWith(String prefix)
- boolean endsWith(String suffix)
- boolean contains(String str)
- replace
- String replace(char oldChar, char newChar)
- String replace(CharSequence oldChar, CharSequence newChar)
- public String trim()
Creating a StringBuilder
(4)
- StringBuilder sb1 = new StringBuilder();
- StringBuilder sb2 = new StringBuilder(“animal”);
- StringBuilder sb3 = new StringBuilder(10);
- Default Capacity of StringBuilder is 16
StringBuilder Methods
(10)
- int length()
- char charAt(int index)
- indexOf
- int indexOf(char ch)
- int indexOf(char ch, index fromIndex)
- int indexOf(String str)
- int indexOf(String str, index fromIndex)
- substring
- String substring(int beginIndex)
- String substring(int beginIndex, int endIndex)
- StringBuilder append(String str)
- StringBuilder insert(int offset, String str)
- StringBuilder delete(int start, int end)
- StringBuilder deleteCharAt(int index)
- StringBuilder reverse()
- String toString()
StringBuilder vs. 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.
Creating Java Arrays
(8)
- int[] numbers1 = new int[3];
- int[] numbers2 = new int[] {42, 55, 99};
- int[] numbers2 = {42, 55, 99};
- int[] numAnimals;
- int [] numAnimals2;
- int numAnimals3[];
- int numAnimals4 [];
- The equals() method on arrays does not look at the elements of the array.
Array Methods
- length (variable not Method)
- java.util.Arrays.sort(array)
- java.util.Arrays.binarySearch(array, element) ;
Creating an ArrayList
(6)
- ArrayList list1 = new ArrayList();
- ArrayList list2 = new ArrayList(10);
- ArrayList list3 = new ArrayList(list2);
- ArrayList<e> list4 = new ArrayList<e>();</e></e>
- ArrayList<e> list5 = new ArrayList<>(); </e>
- as in all generics E can’t be a primitive type
ArrayList Methods
(11)
- boolean add(E element)
- void add(int index, E element)
- boolean remove(Object object)
- E remove(int index)
- E set(int index, E newElement)
- boolean isEmpty()
- int size()
- void clear()
- boolean contains(Object object)
- boolean equals(Object object) (same elements, same order)
- java.util.Collections.sort(numbers);
Wrapper Classes
(8 + 2)
- boolean new Boolean(true)
- byte new Byte((byte)
- short new Short((short)
- int new Integer(1)
- long new Long(1)
- float new Float(1.0)
- double new Double(1.0)
- char new Character(‘c’)
int primitive = Integer.parseInt(“123”);
Integer wrapper = Integer.valueOf(“123”);
Autoboxing
ince Java 5, you can just type the primitive value and Java will convert it to the relevant wrapper class for you.
Converting Between array and List
(2)
- List list = new ArrayList<>();
Object[] objectArray = list.toArray(); - String[] array = { “hawk”, “robin” };
List list = java.util.Arrays.asList(array);
LocalDate
(4)
LocalDate Contains just a date—no time and no time zone. A good example of LocalDate is your birthday this year. It is your birthday for a full day regardless of what time it is.
LocalDate.now();
LocalDate date1 = LocalDate.of(2015, Month.JANUARY, 20);
LocalDate date2 = LocalDate.of(2015, 1, 20);