3 Strings Flashcards Preview

Java OCA flash cards > 3 Strings > Flashcards

Flashcards in 3 Strings Deck (25):
1

Is the new operator required for Strings?

no, it's optional

2

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.

3

What are 13 methods of the String class?

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

4

What are 10 methods of the StringBuilder class?

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

5

What is the difference between StringBuilder & StringBuffer?

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

6

What does == do with two Strings?

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

7

What does == do with two StringsBuilder references?

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

8

What does equals() do on String objects?

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

9

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.

10

Is array size fixed on creation?

yes

11

What does Arrays.sort(myArray) do?

sorts an array

12

Arrays.binarySearch()

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.

13

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

T

14

Can an ArrayList contain primitives?

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

15

What is the method to sort an ArrayList?

Collections.sort()

16

x

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.

17

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();

18

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.

19

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

20

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

21

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?

22

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).

23

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

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

24

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

replace creates a new String object

25

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

If there is no change, replace returns the same object