• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/33

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

33 Cards in this Set

  • Front
  • Back
Why are String objects immutable?
Because the value is stored as private final array of characters.
When is a new objects created for a String (describe 2 scenarios)
When a String is created using keyword new and when a String is created by other method and the passed value is not present in String Constant Pool
What are the 3 ways to create a String
1) Using keyword new; 2) using = sign; 3) using double quote marks around the value, for example passed to a method or in value comparison statements.
What is String Constant Pool and when is it populated
String Constant Pool (or String Pool) helps save memory resources as it stores all String objects created using assignment symbol and double quotes, without repeating them. So when a new String is created using those methods Java first checks whether an object with that value exists in String Pool already, and if it doesn't , the object is created and variable refers to it, otherwise the variable will point to an existing object with passed value.
Name at least 4 possible argument types for String() constructor
1) Text in double quotes 2) array of characters 3) StringBuffer object, 4) StringBuilder object 5) No arguments (which creates an empty string)
What String methods modify the String value?
None. String methods always return a new object with modified value of original String.
What type of argument does charAt() take in String class and what's a possible exception thrown?
charAt() takes any integer values (or values resolvable to int, e.g. char) and throws run-time exception if the index passed is greater than length of String minus 1
What type of argument does indexOf() in String class take and what's the output when the value is not found
indexOf() takes char and String values as arguments, and returns negative 1 if not found
What versions of substring() method exist and what's the possible exception thrown in String class?
substring() can take 1 integer for starting point and returns the rest of the string starting from that point; substring() can take 2 integers for starting and ending positions, and will return the characters not including the end position. Remember the length of returned element is end integer minus start integer. Run time exception will occur if start or end is out of bounds.
What does trim() method do in String class?
Trim() returns a String without leading and trailing spaces, including new lines and tabs
What versions of replace() method exist in String class?
replace() method takes 2 character arguments, or 2 String arguments, but not a mix of those two.
What arguments do startsWith() and endsWith() methods take in String class?
startsWith() and endsWith() take String arguments. Remember String startsWith() a String, and endsWith() a String. The value returned is Boolean.
How does method chaining work for Strings?
String methods chained are read from left to write.
Can you call methods on String constructor used with keyword new? If so, can that constructor have no arguments?
Yes to both. You can call new String() with or without arguments and call methods on those objects. If the argument is not passed to constructor, the code will still compile and no error will be thrown during runtime.
How does concatenation work for Strings with plus sign?
If at least one operand is a String, plus sign concatenates the values into a String. The evaluation of the statement starts on the left, so if the first operand is a String, all the other values will be concatenated together.
When does equals() method return true for comparison of Strings?
When a) it's the same object or b) when it's a String object and the sequence of characters is the same.
When does == (double equals) operator return true for comparison of Strings?
When the variables and/or literals compared refer to the same object. For example they were initialized using assignment operator or double quotes and the value is on String pool
What's the recommended way to compare String objects?
Use equals() method. the double = sign is not recommended due to its limitations.
What is the benefit of using StringBuilder objects instead of String?
StringBuilder objects are mutable. The char array is not final, so it can be modified, which saves on the amount of created objects.
Describe 4 StringBuilder constructors and objects they create
1) No arguments - empty array with default 16 capacity. 2) StringBuilder argument - same array and capacity as passed object. 3) Integer - sets initial capacity to empty array of characters. 4) String or String literal - sets value to value of String and capacity equals length of string + 16 of buffer.
What argument does StringBuilder indexOf() method take?
Only string and optionally a second argument - integer. Char throws compilation error.
What arguments does StringBuilder append() method take
All 8 primitive types, array of characters, String, StringBuffer and any Java API object
What is the default implementation of toString() method in Java objects
Name of the class followed by @ (at) sign followed by hexadecimal representation of the hash of the object.
What arguments does StringBuilder insert() method take?
The first argument is always the offset integer, the second argument could be a primitive value to insert, a String, StringBuffer, array of Characters, CharSequence object, or any other Java object. For array of Characters the optional following arguments are startPosition and length of subarray.
What methods delete characters from StringBuilder objects and how do they work?
delete() method takes 2 integers and works like substring() method, not removing the endPosition character. deleteCharAt() method takes a single integer and removes the character at that position.
What does trim() method do for StringBuilder objects?
There is no method trim() defined for StrinbBuilder class
What is the difference between functionality of most String methods and StringBuilder methods?
String methods do not modify the String object they are called on, however, calling a method on a StringBuilder object, will modify that particular object if they return StringBuilder type.
How is StringBuilder replace() method different from the same method in String class?
It takes 2 integers for start and end position of replacement with the provided String as third argument. The String can be longer than the removed characters between positions. The end position index is not touched.
How does StringBuilder subSequence() method work?
It takes start and end position integers as subString(), but returns an object of type CharSequence. It doesn't modify the original object.
What is the difference between StringBuffer and StringBuilder and when you should use either?
StringBuffer is defined with the same methods as StringBuilder, but they are synchronized, which means they can be accessed by multiple threads, but that leads to overhead. If your code needs access from multiple threads, use StringBuffer, otherwise, it's simpler to stick to StringBuilder.

Can a character object be assigned to a String variable?

No. Causes compilation error.

Is it correct to say that "all string literals are automatically instantiated into a string object"

Yes, according to tests online.

If you print the String object that hasn't been initialized or assigned null, what will show up?

The literal "null"