Chapter 3 - Core Java APIs Flashcards

1
Q

String Concatenation

(3)

A
  1. If both operands are numeric, + means numeric addition.
  2. f either operand is a String, + means concatenation.
  3. The expression is evaluated left to right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

(String) Immutability

(1)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The String Pool

(2)

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

String Methods

(12)

A
  1. int length()
  2. char charAt(int index)
  3. indexOf
    • int indexOf(char ch)
    • int indexOf(char ch, index fromIndex)
    • int indexOf(String str)
    • int indexOf(String str, index fromIndex)
  4. substring
    1. String substring(int beginIndex)
    2. String substring(int beginIndex, int endIndex)
  5. String toLowerCase(String str)
  6. String toUpperCase(String str)
  7. equals
    • boolean equals(String str)
    • boolean equalsIgnoreCase(String str)
  8. boolean startsWith(String prefix)
  9. boolean endsWith(String suffix)
  10. boolean contains(String str)
  11. replace
    • String replace(char oldChar, char newChar)
    • String replace(CharSequence oldChar, CharSequence newChar)
  12. public String trim()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Creating a StringBuilder

(4)

A
  1. StringBuilder sb1 = new StringBuilder();
  2. StringBuilder sb2 = new StringBuilder(“animal”);
  3. StringBuilder sb3 = new StringBuilder(10);
  4. Default Capacity of StringBuilder is 16
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

StringBuilder Methods

(10)

A
  1. int length()
  2. char charAt(int index)
  3. indexOf
    1. int indexOf(char ch)
    2. int indexOf(char ch, index fromIndex)
    3. int indexOf(String str)
    4. int indexOf(String str, index fromIndex)
  4. substring
    1. String substring(int beginIndex)
    2. String substring(int beginIndex, int endIndex)
  5. StringBuilder append(String str)
  6. StringBuilder insert(int offset, String str)
  7. StringBuilder delete(int start, int end)
  8. StringBuilder deleteCharAt(int index)
  9. StringBuilder reverse()
  10. String toString()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

StringBuilder vs. StringBuffer

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Creating Java Arrays

(8)

A
  1. int[] numbers1 = new int[3];
  2. int[] numbers2 = new int[] {42, 55, 99};
  3. int[] numbers2 = {42, 55, 99};
  4. int[] numAnimals;
  5. int [] numAnimals2;
  6. int numAnimals3[];
  7. int numAnimals4 [];
  8. The equals() method on arrays does not look at the elements of the array.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Array Methods

A
  1. length (variable not Method)
  2. java.util.Arrays.sort(array)
  3. java.util.Arrays.binarySearch(array, element) ;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Creating an ArrayList

(6)

A
  1. ArrayList list1 = new ArrayList();
  2. ArrayList list2 = new ArrayList(10);
  3. ArrayList list3 = new ArrayList(list2);
  4. ArrayList<e> list4 = new ArrayList<e>();</e></e>
  5. ArrayList<e> list5 = new ArrayList&lt;&gt;(); </e>
  6. as in all generics E can’t be a primitive type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

ArrayList Methods

(11)

A
  1. boolean add(E element)
  2. void add(int index, E element)
  3. boolean remove(Object object)
  4. E remove(int index)
  5. E set(int index, E newElement)
  6. boolean isEmpty()
  7. int size()
  8. void clear()
  9. boolean contains(Object object)
  10. boolean equals(Object object) (same elements, same order)
  11. java.util.Collections.sort(numbers);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Wrapper Classes

(8 + 2)

A
  1. boolean new Boolean(true)
  2. byte new Byte((byte)
  3. short new Short((short)
  4. int new Integer(1)
  5. long new Long(1)
  6. float new Float(1.0)
  7. double new Double(1.0)
  8. char new Character(‘c’)

int primitive = Integer.parseInt(“123”);

Integer wrapper = Integer.valueOf(“123”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Autoboxing

A

ince Java 5, you can just type the primitive value and Java will convert it to the relevant wrapper class for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Converting Between array and List

(2)

A
  1. List list = new ArrayList<>();
    Object[] objectArray = list.toArray();
  2. String[] array = { “hawk”, “robin” };
    List list = java.util.Arrays.asList(array);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

LocalDate

(4)

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

LocalTime

(5)

A
  • *LocalTime** Contains just a time—no date and no time zone. A good example of LocalTime is midnight. It is midnight at the same time every day.
  • *LocalTime.now();**

LocalTime time1 = LocalTime.of(6, 15);

LocalTime time2 = LocalTime.of(6, 15, 30);

LocalTime time3 = LocalTime.of(6, 15, 30, 200);

17
Q

LocalDateTime

(9)

A

LocalDateTime Contains both a date and time but no time zone. A good example of LocalDateTime is “the stroke of midnight on New Year’s.” Midnight on January 2 isn’t nearly as special, and clearly an hour after midnight isn’t as special either.

java.time.LocalDateTime.now();

public static LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute)

public static LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second)

public static LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanos)

public static LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute)

public static LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second)

public static LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanos)

public static LocalDateTime.of(LocalDate date, LocalTime)

18
Q

Date Methods

A

Method LocalDate LocalTime LocalDateTime plus/minusYears yes no yes plus/minusMonths yes no yes plus/minusWeeks yes no yes plus/minusDays yes no yes plus/minusHours no yes yes plus/minusMinutes no yes yes plus/minusSeconds no yes yes plus/minusNanos no yes yes toEpochDay() yes no no toEpochTime() no no yes

19
Q

Periods

A

Period annually = Period.ofYears(1); // every 1 year

Period quarterly = Period.ofMonths(3); // every 3 months

Period everyThreeWeeks = Period.ofWeeks(3); // every 3 weeks

Period everyOtherDay = Period.ofDays(2); // every 2 days

Period everyYearAndAWeek = Period.of(1, 0, 7);// every year and 7 days

20
Q

Date Formatting

A

LocalDateTime dateTime = LocalDateTime.of(date, time);

System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));

System.out.println(time.format(DateTimeFormatter.ISO_LOCAL_TIME)); System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);

System.out.println(shortDateTime.format(dateTime)); // 1/20/20 System.out.println(shortDateTime.format(date)); // 1/20/20

System.out.println( shortDateTime.format(time)); // UnsupportedTemporalTypeException

MMMM M represents the month. The more Ms you have, the more verbose the Java output. For example, M outputs 1, MM outputs 01, MMM outputs Jan, and MMMM outputs January.

dd d represents the date in the month. As with month, the more ds you have, the more verbose the Java output. dd means to include the leading zero for a single-digit month.

  • *,** Use , if you want to output a comma (this also appears after the year).
  • *yyyy** y represents the year. yy outputs a two-digit year and yyyy outputs a four-digit year.

hh h represents the hour. Use hh to include the leading zero if you’re outputting a single- digit hour.

: Use : if you want to output a colon. mm m represents the minute.

21
Q

Parsing Dates and Time

A

DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”);

LocalDate date = LocalDate.parse(“01 02 2015”, f);
LocalTime time = LocalTime.parse(“11:22”);