3 Strings Flashcards Preview

Java OCA flash cards > 3 Strings > Flashcards

Flashcards in 3 Strings Deck (25):

Is the new operator required for Strings?

no, it's optional


In a string, When does + concat and when does it perform addition?

If either operand involved in the + expression is a String, concatenation is used; otherwise, addition is used.


What are 13 methods of the String class?

cceeeilrssttt charAt(), concat(), endsWith(), equals(), equalsIgnoreCase(), indexOf(), length(), replace(), startsWith(), substring(), toLowerCase(), toUpperCase(), and trim()


What are 10 methods of the StringBuilder class?

append(), charAt(), delete(), deleteCharAt(), indexOf(), insert(), length(), reverse(), substring(), toString().


What is the difference between StringBuilder & StringBuffer?

StringBuffer is the same as StringBuilder except that it is thread safe


What does == do with two Strings?

Calling == on String objects will check whether they point to the same object in the pool


What does == do with two StringsBuilder references?

Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object


What does equals() do on String objects?

Calling equals() on String objects will check whether the sequence of characters is the same.


What does equals() do on StringBuilder objects?

Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.


Is array size fixed on creation?



What does Arrays.sort(myArray) do?

sorts an array



searches a sorted array and returns the index of a match. If no match is found, it negates the position where the element would need to be inserted and subtracts 1.


T or F Methods that are passed varargs (…) can be used as if a normal array was passed in.



Can an ArrayList contain primitives?

No, primitives must be in wrapper classes. Java will autobox parameters passed in to the proper wrapper type.


What is the method to sort an ArrayList?




A LocalDate contains just a date, a LocalTime contains just a time, and a LocalDateTime contains both a date and time. All three have private constructors and are created using LocalDate.now() or LocalDate.of() The Period class represents a number of days, months, or years to add or subtract from a LocalDate or LocalDateTime. DateTimeFormatter is used to output dates and times in the desired format. The date and time classes are all immutable, which means the return value must be used.


Is an array measured by size or length?

Arrays use length and it is a property not a method Example twoD.length is correct. Do NOT use twoD.length();


What's the difference? String name = "Fluffy"; String name = new String("Fluffy");

The first one is in the string pool. second is a new object.


What are the rules for concatenation vs addition?

1. If both operands are numeric, + means numeric addition. 2. If either operand is a String, + means concatenation. 3. The expression is evaluated left to right. Now let’s look at some examples: System.out.println(1 + 2); // 3 System.out.println("a" + "b"); // ab System.out.println("a" + "b" + 3); // ab3 System.out.println(1 + 2 + "c"); // 3c


What are the two method signatures for String's replace() method?

* It replaces ALL instances. System.out.println("abcabc".replace('a', 'A')); // AbcAbc System.out.println("abcabc".replace("a", "A")); // AbcAbc It can replace 1 character or a whole sequence but it will replace ALL instances of a match. animalsssss.replace('s','z') --> animalzzzzz animalsssss.replace("ss","z") --> animalzzzzs


6 facts on Strings

These are the six facts on Strings: 1. Literal strings within the same class in the same package represent references to the same String object. 2. Literal strings within different classes in the same package represent references to the same String object. 3. Literal strings within different classes in different packages likewise represent references to the same String object. 4. Strings computed by constant expressions are computed at compile time and then treated as if they were literals. 5. Strings computed at run time are newly created and therefore are distinct. (So line 4 prints false.) 6. The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents. (So line 5 prints true.) When a variable and literal are concatenated at run time it is not the same object. Consider following classes: //In File Other.java package other; public class Other { public static String hello = "Hello"; } //In File Test.java package testPackage; import other.*; class Test{ public static void main(String[] args){ String hello = "Hello", lo = "lo"; System.out.print((testPackage.Other.hello == hello) + " "); //line 1 System.out.print((other.Other.hello == hello) + " "); //line 2 System.out.print((hello == ("Hel"+"lo")) + " "); //line 3 System.out.print((hello == ("Hel"+lo)) + " "); //line 4 System.out.println(hello == ("Hel"+lo).intern()); //line 5 } } class Other { static String hello = "Hello"; } What will be the output of running class Test?


Is String a final class?

Yes and so are these: StringBuffer and StringBuilder are also final. All Primitive wrappers are also final (i.e. Boolean, Integer, Byte etc).


Can string be subclassed? Is this legal? class MyString extends String{ MyString(){ super(); } }

No, String is a final class. It cannot be subclassed (extended)


Why is this false? "String".replace('g','G')=="String".replace('g','G');

replace creates a new String object


Why is this true? "String".replace('g','g')=="String".replace('g','g');

If there is no change, replace returns the same object