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]