Core APIs Flashcards
STRING: int length()
Returns the length of the string
STRING: char charAt(int index)
Returns the character at the specified index in the string.
STRING: int indexOf(int ch, int fromIndex)
Returns the index of the first occurrence of the specified character, starting the search at fromIndex.
STRING: int indexOf(String str)
Returns the index of the first occurrence of the specified substring.
STRING: String substring(int beginIndex)
Returns a new string that is a substring of this string, starting from beginIndex to the end.
STRING: int indexOf(String str, int fromIndex)
Returns the index of the first occurrence of the specified substring, starting the search at fromIndex.
STRING: String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string, starting from beginIndex and ending at endIndex - 1.
STRING: String toLowerCase()
Converts all characters in the string to lowercase.
STRING: String toUpperCase()
Converts all characters in the string to uppercase.
STRING: boolean equals(Object obj)
Compares this string to the specified object for equality.
STRING: boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
STRING: boolean equalsIgnoreCase(String str)
Compares this string to another string, ignoring case considerations.
STRING: boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.
STRING: boolean contains(CharSequence charSeq)
Returns true if and only if this string contains the specified sequence of characters.
STRING: String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
STRING: String replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified replacement sequence.
How to return the length of the string?
int length()
How to get the character at a specific index in the string?
char charAt(int index)
How to find the index of a character, starting the search from a specific index?
int indexOf(int ch, int fromIndex)
How to find the index of the first occurrence of a substring?
int indexOf(String str)
How to find the index of a substring, starting the search from a specific index?
int indexOf(String str, int fromIndex)
How to get a substring from a specific index to the end of the string?
String substring(int beginIndex)
How to get a substring between two specified indices?
String substring(int beginIndex, int endIndex)
How to convert all characters in the string to lowercase?
String toLowerCase()
How to convert all characters in the string to uppercase?
String toUpperCase()
How to compare this string to another object for equality?
boolean equals(Object obj)
How to compare this string to another string, ignoring case?
boolean equalsIgnoreCase(String str)
How to check if a string starts with a specific prefix?
boolean startsWith(String prefix)
How to check if a string ends with a specific suffix?
boolean endsWith(String suffix)
How to check if a string contains a specific sequence of characters?
boolean contains(CharSequence charSeq)
How to replace all occurrences of a character with another character in a string?
String replace(char oldChar, char newChar)
How to replace all occurrences of a target sequence with a replacement sequence in a string?
String replace(CharSequence target, CharSequence replacement)
How to remove whitespace from both ends of a string?
String strip()
How to remove whitespace from the end of a string?
String stripTrailing()
How to remove whitespace from the beginning of a string?
String stripLeading()
How to add a specified number of spaces at the beginning of a string?
String indent(int numberSpaces)
How to remove whitespace from both ends of a string (alternative to strip())?
String trim()
How to remove leading whitespace from every line in a string?
String stripIndent()
How to return a string with escape sequences translated?
String translateEscapes()
How to check if a string is empty (length of 0)?
boolean isEmpty()
How to check if a string is empty or contains only whitespace characters?
boolean isBlank()
STRING: String stripLeading()
Removes whitespace from the beginning of the string.
How to create a formatted string using a format string and arguments?
static String format(String format, Object args…)
How to create a formatted string using a specific locale, format string, and arguments?
static String format(Locale loc, String format, Object args…)
STRING: String stripTrailing()
Removes whitespace from the end of the string.
How to create a formatted string using this string as the format string?
String formatted(Object args…)
STRING: String strip()
Removes whitespace from both the beginning and end of the string.
STRING: String trim()
Removes whitespace from both the beginning and end of the string (similar to strip()).
STRING: String indent(int numberSpaces)
Adds the specified number of spaces to the beginning of each line in the string. Can use negative numbers to remove spaces.
STRING: String translateEscapes()
Translates escape sequences in the string (like \n, \t) into their corresponding characters.
STRING: String stripIndent()
Removes any leading whitespace from every line in the string.
STRING: boolean isEmpty()
Returns true if the string has a length of 0, otherwise false.
STRING: boolean isBlank()
Returns true if the string is empty or contains only whitespace characters, otherwise false.
STRING: static String format(String format, Object args…)
Creates a formatted string using a format string and arguments.
STRING: static String format(Locale loc, String format, Object args…)
Creates a formatted string using a specific locale, format string, and arguments.
STRING: String formatted(Object args…)
Creates a formatted string using this string as the format string and the provided arguments.
STRING BUILDER: append()
Adds characters to the end of the StringBuilder.
STRING BUILDER: insert(int offset, String str)
Inserts a string at the specified position in the StringBuilder. Remember it changes indexes!
STRING BUILDER: delete(int startIndex, int endIndex)
Removes the characters in a substring of this StringBuilder. The substring begins at the specified startIndex and extends to the character at index endIndex - 1. It supports indexes larger than its string length. Remember it changes indexes!
STRING BUILDER: deleteCharAt(int index)
Removes the character at the specified position in this StringBuilder. It supports indexes larger than its string length. Remember it changes indexes!
STRING BUILDER: replace(int startIndex, int endIndex, String newString)
Replaces the characters in a substring of this StringBuilder with characters in the specified String.
STRING BUILDER: reverse()
Causes this character sequence to be replaced by the reverse of the sequence.
How can we add characters to the end of a StringBuilder?
append()
How can we insert a string at a specific position in a StringBuilder?
insert(int offset, String str)
How can we remove a range of characters from a StringBuilder?
delete(int startIndex, int endIndex)
How can we remove a single character at a specific index from a StringBuilder?
deleteCharAt(int index)
How can we replace a range of characters in a StringBuilder with a new string?
replace(int startIndex, int endIndex, String newString)
How can we reverse the entire sequence of characters in a StringBuilder?
reverse()
How are strings interned?
On compile time automatically when in format of string literals. If in format of new String(), then at runtime. Also, can be manually by calling intern() method on string.
How do we check for string reference equality?
With ==, but only on interned strings
How do we check for string equality?
With equals()
Can arrays and primitives be initialized in same line?
Yes. eg. int ids[] = {}, types = 7;
What do arrays actually contain?
They contain references to objects and primitives
How do we initialize new string array?
String names[] = new String[2];
How can we initialize two dimensional array?
String[] names[] = new String[][]{new String[2]};
String names[][] = new String[][]{new String[2]};
String[][] names = new String[][]{new String[2]};
How do we get size of array?
length. Without parentheses.
How do we order arrays?
Arrays.sort(…)
How do se search arrays?
Arrays.binarySearch(haystack, needle)
But array must be sorted for it to work!
What are rules for Arrays.binarySearch?
- array must be sorted. if not sorted, result will be random
- if is sorted and needle is found, result will be positive
- if is sorted and needle is not found, result is -(index-1)
How do we compare arrays?
Arrays.compare(…)
What are rules for arrays comparing?
generally, if first array is “superior”, result is positive:
- if all is identical, result is 0
- if all is the same, but second array is bigger, result is negative
- if all is the same, but first is bigger, result is positive
- if first difference in first array is smaller, then result is negative
- if first difference in first array is bigger, result is positive
- only two same types of array can be compared, or else exception is thrown
What are rules for defining what values are bigger or smaller in array comparing?
- capital letters
- regular letters - ab, ba
- numbers - numbers in order
- null
MATH: Math.random()
Returns a double value between 0.0 (inclusive) and 1.0 (exclusive)
How can we generate a random number between 0 and 1?
double result = Math.random();
MATH: Math.pow(double base, double exponent)
Returns a double value that is the first argument raised to the power of the second argument
How can we calculate a number raised to a power?
double result = Math.pow(double base, double exponent);
MATH: Math.floor(double num)
Returns a double value that is the largest integer less than or equal to the argument
How can we round down a decimal number to the nearest integer?
double result = Math.floor(double num);
MATH: Math.ceil(double num)
Returns a double value that is the smallest integer greater than or equal to the argument
How can we round a float to the nearest integer?
int result = Math.round(float num);
How can we round up a decimal number to the nearest integer?
double result = Math.ceil(double num);
MATH: Math.round(float num)
Returns an int value that is the argument rounded to the nearest integer
MATH: Math.round(double num)
Returns a long value that is the argument rounded to the nearest integer
How can we round a double to the nearest integer?
long result = Math.round(double num);
MATH: Math.min(long a, long b)
Returns the long value that is the smaller of the two arguments
MATH: Math.min(double a, double b)
Returns the double value that is the smaller of the two arguments
How can we find the minimum of two long integers?
long result = Math.min(long a, long b);
MATH: Math.min(int a, int b)
Returns the int value that is the smaller of the two arguments
How can we find the minimum of two integers?
int result = Math.min(int a, int b);
MATH: Math.min(float a, float b)
Returns the float value that is the smaller of the two arguments
DATE TIME: How can we find the minimum of two double precision numbers?
double result = Math.min(double a, double b);
How can we find the minimum of two float numbers?
float result = Math.min(float a, float b);
DATE TIME: Are LocalDateTime and similar classes mutable?
No
DATE TIME: Can LocalDateTime and similar classes be instantiated?
No, only by factory methods
DATE TIME: How can you get current date?
LocalDate.now()
DATE TIME: How can you get specific date?
LocalDate.of(int year, int month, int dayOfMonth)
DATE TIME: How can you get current date and time?
LocalDateTime.now()
DATE TIME: How can you get current time?
LocalTime.now()
DATE TIME: How can you get current zoned date and time?
ZonedLocalDateTime.now()
DATE TIME: How can you get specific zoned date and time?
Same as LocalDateTime, but with ZoneId at the end
DATE TIME: How can you get specific time?
LocalTime.of(int hour, int minute, int second)
DATE TIME: What do you need to watch out when using “of()” method?
Not to mix time an and date values where they don’t belong:
var date = LocalDate.of(2022, Month.JANUARY, 42);
DATE TIME: How can you get specific date and time?
LocalDateTime.of(int year, int month, int dayOfMonth, int hour, int minute, int second)
DATE TIME: How can you check if some date or time is before other date or time?
isBefore() method
DATE TIME: How can you define a period?
Period.ofYears(), Period.ofMonths(), Period.ofWeeks(), Period.ofDays()
Period.of(int years, int months, int days)
DATE TIME: Where can you use Period?
On LocalDate and LocalDateTime classes
DATE TIME: How can you calculate or add period?
date.plus(period)
DATE TIME: How can you define duration?
Duration.ofDays() … Duration.ofNanos()
Duration.of(int number, ChronoUnit)
DATE TIME: What is Duration used for?
To quickly calculate duration between two temporals
DATE TIME: How can you get difference between two temporals?
ChronoUnit.DAYS.between(Temporal first, Temporal second, ChronoUnit)
Duration.between(Temporal first, Temporal second, ChronoUnit)
DATE TIME: How can you add or remove temporals from another temporal?
dateTime.plus(int number, ChronoUnit)
dateTime.minus(int number, ChronoUnit)
DATE TIME: How can you add to Duration?
duration.plusDays(int number)
…
duration.plusSeconds(int number)
DATE TIME: How can you remove from Duration?
duration.minusDays(int number)
…
duration.minusSeconds(int number)
DATE TIME: How can you truncate time?
time.truncatedTo(ChronoUnit)
DATE TIME: What is the difference between LocalDateTime.now() and Instant.now()?
Instant.now() contains zone information
DATE TIME: What is alternative to Instant.now()?
ZonedLocalDateTime.toInstant()
DATE TIME: How can you use Instant()?
To calculate duration: Duration.between(Instant one, Instant two)