Core Java APIs Flashcards
What does 3 + “c” result in?
“3c” (a String)
Is String mutable or immutable?
immutable
Soooo you can concat it with + but there aren’t any String methods to change them.
HOWEVER, you can set it equal to something else to make it point to that instead. It won’t change the original object though, just create a new one
string pool
An area in the JVM where string literals are stored. The JVM can optimize the use of string literals by allowing only one instance of a string in the pool.
What is the difference between
String name = “Fluffy”;
and
String name = new String(“Fluffy”);
The first puts “Fluffy” in the string pool, and the second does not. The second one is less efficient, so do the first.
mystring.length()
Returns the length of mystring
mystring.charAt(i)
Returns the character at the i-th index of mystring
mystring.indexOf(c)
Finds the first index of mystring that matches the char or String c
mystring.indexOf(c, i)
Finds the first index of mystring that matches c starting from index i
What does indexOf() return if it can’t find a match?
-1
mystring.substring(x)
Returns the substring of mystring starting at index x until the end
mystring.substring(x,y)
Returns the substring of mystring starting at index x and ending at index y (meaning don’t actually include the character at index y)
String s = “funtimes”;
return s.substring(3,7);
will return “time”
mystring.toLowerCase()
Returns mystring as all lowercase
does not actually change the String
mystring.toUpperCase()
Returns mystring as all uppercase
does not actually change the String
mystring.equals(yourString)
Returns true if mystring and yourString contain exactly the same characters in the same order. Otherwise returns false.
mystring.equalsIgnoreCase(yourString)
Like mystring.equals() but ignores case
mystring.startsWith(str)
Returns true if mystring starts with str, otherwise returns false
mystring.endsWith(str)
Returns true if mystring ends with str, otherwise returns false
mystring.contains(str)
Returns true if mystring contains str, otherwise returns false
mystring.replace(str1, str2)
Returns mystring with str1 replaced by str2
Note: Can use char or String parameters
mystring.trim()
Returns mystring with white space removed from the beginning and end
StringBuilder
Basically the mutable version of String!
myStringBuilder.append(c)
Appends the character c to myStringBuilder
StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("animal"); StringBuilder sb3 = new StringBuilder(10);
Create empty StringBuilder object
Create StringBuilder object with “animal” as it’s value
Create StringBuilder object with space for ten characters
myStringBuilder.charAt()
myStringBuilder.indexOf()
myStringBuilder.length()
myStringBuilder.substring()
All the same as in the String class
myStringBuilder.insert(n, str)
Inserts str at the n-th index of myStringBuilder
myStringBuilder.delete(i, j)
Deletes characters from myStringBuilder starting at index i and stopping before index j
myStringBuilder.deleteCharAt(i)
Deletes the character of myStringBuilder at the i-th index
myStringBuilder.reverse()
Reverses myStringBuilder
myStringBuilder.toString()
Converts myStringBuilder to a String
How to declare an array of 3 ints
int[] nums = new int[3]
How to delcare an array of 3 specific ints
int[] nums = new int[] {1, 2, 3}
or shorter
int[] nums = {1, 2, 3}
4 ways to put the [] when declaring an array
int[] myArray
int [] myArray
int myArray[]
int myArray []
myArray.length
Returns the number of elements in myArray
What must you import to use the Arrays class?
import java.util.*
or
import java.util.Arrays
int[] nums = {6, 9, 1};
Arrays.sort(nums);
Sorts nums in ascending order
What are the rules of numbers and letters when sorting an array?
Numbers before letters, and uppercase before lowercase
int[] numbers = {2,4,6,8};
System.out.println(Arrays.binarySearch(numbers, 2));
System.out.println(Arrays.binarySearch(numbers, 4));
System.out.println(Arrays.binarySearch(numbers, 1));
System.out.println(Arrays.binarySearch(numbers, 3));
System.out.println(Arrays.binarySearch(numbers, 9));
0 1 -1 -2 -5
Array must be sorted for this to work!
How to import ArrayList?
import java.util.ArrayList;
3 ways to declare an ArrayList (old way)
ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(10); ArrayList list3 = new ArrayList(list2);
ArrayList list = new ArrayList(); list.add("word");
Adds “word” to the ArrayList
How to add an item at a specific index in an ArrayList called list
list.add(idx, item)
How to remove an item from an ArrayList called list
list.remove(item) //returns true if item is in the list, and false if not
or
list.remove(index) //returns the item at that index
How to set a specific index with an element in an ArrayList called list
E set(int index, E newElement)
list.set(i, item)
returns the element that got replaced. Keeps the ArrayList the same size
ArrayList list = new ArrayList();
list. isEmpty();
list. size();
Checks if list is empty (returns a boolean)
Then determines number of elements in list (returns an int)
ArrayList list = new ArrayList(); list.clear()
Removes all elements of list
No return
ArrayList list = new ArrayList(); list.contains(obj);
Checks whether obj is in list.
Returns a boolean
ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList();
list1.equals(list2)
Checks if list1 and list2 have the same elements in the same order
Converting String to a primitive (lets say int for this example)
Integer.parseInt(“10”);
Converting String to a wrapper class (lets say Integer for this example)
Integer.valueOf(“10”);
Autoboxing
The compiler automatically converts a primitive type into the matching wrapper class and vice versa.
Wrapper Class
Integer for int,
Boolean for boolean,
etc.
Convert from an ArrayList of Strings to an array of Strings
ArrayList list = new ArrayList;
String[] stringArray = list.toArray(new String[0]);
The “0” says to pick the same size as the list
Without specifying “new String[0]” the array will be of type Object[]
Convert from an array of Strings to a List of Strings
String[] array = {“hawk”, “robin”};
List list = Arrays.asList(array);
Once it is a List, you cannot change its size
How to sort an ArrayList
ArrayList list = new ArrayList();
Collections.sort(list);
What import statement must you use to work with date and time?
import java.time.*
LocalDate
A class that deals with just the date, no time
LocalTime
A class that deals with just the time, no date
LocalDateTime
A class that deals with both date and time
If the date is June 20th, 2016 at 12:30 pm, what is the output?
System.out.println(LocalDate.now());
System.out.println(LocalTime.now());
System.out.println(LocalDateTime.now());
2016-06-20
12:30:00.000
2016-06-20T12:30:00.000
Create a LocalDate object for June 20th, 2016
LocalDate date1 = LocalDate.of(2016, Month.JUNE, 20);
or
LocalDate date2 = LocalDate.of(2016, 6, 20);
Create a LocalTime object for 12:30:00 pm
LocalTime time = LocalTime.of(12, 30, 0);
Can add one more parameter to specify nanoseconds, or one parameter less to not specify seconds
What is wrong with this?
LocalDate d = new LocalDate();
You can’t create a LocalDate, LocalTime, or LocalDateTime object in this fashion. You gotta use of() or now() or whateva
How to add 3 days, 3 weeks, 3 months, or 3 years to a LocalDate object called date
date = date.plusDays(3);
date = date.plusWeeks(3);
date = date.plusMonths(3);
date = date.plusYears(3);
How to go back 3 days, 3 hours, or 3 seconds to a LocalDateTime object called dateTime
dateTime = dateTime.minusDays(3);
dateTime = dateTime.minusHours(3);
dateTime = dateTime.minusSeconds(3);
Period Object
An objet that can hold a certain period of time
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
How to add a Period called period to a LocalDate object called date
date = date.plus(period);
Can you chain Period methods?
no!
Period wrong = Period.ofYears(1).ofWeeks(1);
is the same as
Period wrong = Period.ofYears(1);
wrong = Period.ofWeeks(1);
What package is the class DateTimeFormatter in?
java.time.format
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
Tell whether the following are legal and what they output
1) f.format(localDate);
2) f.format(localDateTime);
3) f.format(localTime);
1) Legal, shows whole object localDate
2) Legal, shows just the date of localDatetime
3) Throws runtime exception
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
Tell whether the following are legal and what they output
1) f.format(localDate);
2) f.format(localDateTime);
3) f.format(localTime);
1) Throws runtime exception
2) Legal shows whole localDateTime object
3) Throws runtime exception
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
Tell whether the following are legal and what they output
1) f.format(localDate);
2) f.format(localDateTime);
3) f.format(localTime);
1) Throws runtime exception
2) Legal shows only time part
3) Legal shows whole localTime object
What method do you use to create your own DateTimeFormatter pattern?
ofPattern()
When specifying a DateTimeFormatter pattern, what do the following mean?
1) M
2) MM
3) MMM
4) MMMM
5) d
6) dd
7) yy
8) yyyy
9) h
10) hh
11) m
12) mm
1) Represents month as a single digit (or two if 10-12)
2) Represents month as two digits
3) Represents the three letter abbreviation of the month
4) Represents the entire month name
5) Represents day with its default amount of digits
6) Represents day as a two digit number
7) Represents year as a two digit number
8) Represents year as a four digit number
9) Represents hour as its default amount of digits
10) Represents hour as a two digit number
11) Represents minutes as default amount of digits
12) Represents minutes as two digits
DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”);
LocalDate date = LocalDate.parse(“01 02 2015”, f);
LocalTime time = LocalTime.parse(“11:22”);
Parses date using the formatter f
Then parses time using the default formatter for time
Difference between calling equals() on a String and calling equals() on a StringBuilder
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.
Difference between calling equals() on an array and calling equals() on an ArrayList
Calling equals() on an array checks object reference equality
Calling equals() on an ArrayList checks to see if the ArrayLists have the same elements in the same order
Can you append a boolean value to a String?
yup