1/136
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
String
An object that represents a sequence of characters.
Are Strings primitive types or objects?
Objects.
String immutability
Once a String object is created, its contents cannot be changed.
What happens when a String seems to be modified?
A new String object is created instead of changing the original.
How can you create a String using new?
String s = new String("Ricky");
How can you create a String using a literal?
String s = "Ricky";
String literal
Text enclosed in double quotes, such as "Hello".
How do you call a method on an object?
objectReference.methodName(parameterList)
What does length() return?
The number of characters in a String.
If String s = "Ricky", what does s.length() return?
5.
void method
A method that does not return a value.
Concatenation
Combining Strings by appending characters to the end of another String.
What method can concatenate Strings?
concat().
What operator is commonly used for String concatenation?
+.
What does "Hello" + "World!" produce?
"HelloWorld!"
What does "Hello " + "World!" produce?
"Hello World!"
What does s1.concat(s2) do?
Returns a new String containing s1 followed by s2.
Does concat() modify the original String?
No. Strings are immutable, so it returns a new String.
String pool
Special JVM memory area used to store and reuse String literals.
String interning
Reusing an existing String literal from the String pool instead of creating another identical object.
What happens when Java encounters a String literal already in the String pool?
It gives the variable a reference to the existing String object.
Where are String literals generally stored?
The String pool.
Where are Strings created with new stored?
The heap.
Are Strings created using new automatically interned?
No.
Heap
Memory area where objects created using new are stored.
What does new String("Hello") do?
Creates a new String object on the heap.
charAt(int index)
Returns the character at the specified index of a String.
What index does the first character of a String have?
0.
If String s = "Hello", what is s.charAt(0)?
'H'.
If String s = "Hello", what is s.charAt(4)?
'o'.
What happens if charAt() uses an index outside the String?
An index-out-of-bounds exception occurs.
compareTo(String)
Compares two Strings lexicographically and returns 0 when their contents are equal.
What does compareTo() return when two Strings are equal?
0.
equals(Object)
Compares the contents of two Strings.
What does equals() return when two Strings contain the same sequence of characters?
true.
Is equals() case-sensitive?
Yes.
equalsIgnoreCase()
Compares String contents while ignoring uppercase and lowercase differences.
What does "hello".equals("Hello") return?
false.
What does "hello".equalsIgnoreCase("Hello") return?
true.
isEmpty()
Returns true when a String contains zero characters.
What does "".isEmpty() return?
true.
What does " ".isEmpty() return?
false.
Why is " ".isEmpty() false?
The String contains one space character.
System
A Java class.
System.out
An object representing standard output, usually the console.
println()
Prints output and then moves to a new line.
print()
Prints output without automatically moving to a new line.
What is the difference between print() and println()?
println() adds a newline; print() does not.
What does System.out.print("Hi"); do?
Prints Hi without automatically starting a new line.
What does System.out.println("Hi"); do?
Prints Hi and then starts a new line.
Escape sequence
A special character sequence beginning with a backslash that controls or represents special output.
What character begins a Java escape sequence?
Backslash \.
What does \n represent?
A newline.
What does \t represent?
A tab.
What does \" represent?
A double quotation mark inside a String.
What does \\ represent?
A single backslash character.
How do you include quotation marks inside a String?
Escape them with \", such as "He said \"Hi\"".
What does System.out.println("Hello\nWorld"); print?
Hello and World on separate lines.
What does System.out.print("A\tB"); do?
Prints A and B separated by a tab.
printf()
Prints formatted output using format specifiers.
Format specifier
A placeholder in printf that is replaced by a supplied value.
Why use printf instead of print or println?
To control how values are formatted in output.
What format specifier is commonly used for an int?
%d.
What format specifier is commonly used for a floating-point value?
%f.
What format specifier is commonly used for a String?
%s.
What does %.2f mean in printf?
Display a floating-point value with 2 digits after the decimal.
Does printf automatically add a newline?
No.
How can printf include a newline?
Use %n or \n in the format String.
What does System.out.printf("%d", 5); print?
5.
What does System.out.printf("%.2f", 3.14159); print?
3.14.
What does System.out.printf("%s", "Hello"); print?
Hello.
Variable aliasing
When multiple reference variables refer to the same object.
What do object variables store?
References to objects.
What do primitive variables store?
Their actual primitive values.
If String a = "Suzy"; String b = "Mark"; is a == b true?
false.
After a = b, what object does a reference?
The same String object referenced by b.
After a = b, what does a == b return?
true.
Why does a == b become true after a = b?
Both variables now contain the same object reference.
What does == compare for String objects?
Whether the two references point to the same object.
What does equals() compare for Strings?
The characters or contents of the Strings.
Main difference between == and equals() for Strings
== compares references; equals() compares contents.
Should == normally be used to compare String contents?
No. Use equals().
String c = new String("Mary"); String d = new String("Mary"); What is c == d?
false.
Why is c == d false when both contain "Mary"?
new creates two separate objects with different references.
String c = new String("Mary"); String d = new String("Mary"); What is c.equals(d)?
true.
Why is c.equals(d) true?
Both Strings contain the same sequence of characters.
Is equals() case-sensitive?
Yes.
What method compares Strings while ignoring case?
equalsIgnoreCase().
If String str1 = "Hello"; String str2 = "Hello"; what is str1 == str2?
true.
Why can two identical String literals have == return true?
Java may intern both literals so they reference the same String pool object.
If str1 = "Hello" and str3 = new String("Hello"), what is str1 == str3?
false.
If str1 = "Hello" and str3 = new String("Hello"), what is str1.equals(str3)?
true.
String str1 = "Hello"; String str2 = "Hello"; What is str1.equals(str2)?
true.
String str4 = "Hello".toLowerCase(); String str5 = "hello"; Is str4 == str5 guaranteed true?
No.
Why should you not use == to compare str4 and str5?
Their contents may match even though their references may differ.
How should str4 and str5 be compared for matching contents?
str4.equals(str5).
What does toLowerCase() do?
Returns a new String with letters converted to lowercase.
Does toLowerCase() modify the original String?
No. Strings are immutable.
If String s = "HELLO"; s.toLowerCase(); what is s afterward?
"HELLO".
Why does s remain "HELLO" after s.toLowerCase();?
The returned new String was not assigned anywhere.