CH4 - Core APIs Flashcards
String concatenation rules
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + mean concatenation.
- The expression is evaluated left to right.
StringBuilder replace method
StringBuilder replace(int start, int end, String str)
start – The starting index (inclusive) of the substring to be replaced.
end – The ending index (exclusive) of the substring to be replaced.
str – The string that will replace the selected portion.
Returns the modified StringBuilder object.
✅ Inclusive start, exclusive end
✅ The replacement does not have to be the same length as the removed text.
✅ If str is “”, it removes the selected portion.
✅ replace() modifies the existing StringBuilder object (does not return a new instance).
In Java, an int and long can be implicitly converted to double.
In Java, an int and long can be implicitly converted to double.
System.out.println(numbers.substring(7, 7));
Results empty String
binarySearch() method
The binarySearch() method looks at where a
value would be inserted, which is before the second
element for Pippa. It then negates it and subtracts one
String intern() method
🔹 The String Pool is a part of the heap but is a special memory area managed by the JVM.
🔹 intern() moves the string to the pool only if it is not already there and returns the reference from the pool.
🔹 If the string already exists in the pool, intern() just returns the existing reference instead of creating a new object.
🔹 String literals (“Java”) are automatically stored in the pool at compile time, while new String(“Java”) creates a separate object in the heap.
🔹 s1 == s2 can be false because s1 (created with new String()) is in the heap, while s2 (returned by intern()) is in the pool.
🔹 Using intern() can save memory when dealing with many duplicate strings by ensuring only one copy is stored in the pool.
String format() method formatting symbols
%s - applies to any type, commonly String values
%d - applies to integer values like int and long
%f - applies to floating-point values like float and double
%n - inserts a line break using the system-dependent line separator
String format() method extra
-format() method relies on rounding rather than truncating
StringBuilder delete() method
-delete (int startIndex, int endIndex)
-deleteCharAt(int index)
var name = “Hello World”; var name2 = new String(“Hello World”).intern(); System.out.println(name == name2);
What is the output of this ?
True
BigInteger, BigDecimal
Both classes are immutable.
Both classes provide constants for the most common values like BigInteger.ZERO and BigDecimal.ONE
LocalDate
Contains just a date - no time and no time zone.
LocalTime
Contains just a time - no date and no time zone.
LocalDateTime
Contains both a date and time but no time zone.
ZonedDateTime
Contains a date, time and time zone.
Example: “Conference call at 9 a.m. EST”
Period Format
P1Y2M3D
Period
#years
#months
#days
Period.ofMonths(3) -> P3M
Instant class
The Instant class in Java is part of the java.time package and represents a point in time, measured in nanoseconds from the epoch (January 1, 1970, 00:00:00 UTC). It is useful for working with timestamps in applications.
Key Concepts of Instant
-Immutable & Thread-Safe – Instant is immutable and thread-safe, making it ideal for concurrent applications.
-UTC-Based – It represents a timestamp independent of time zones.
-Nanosecond Precision – It stores time with nanosecond accuracy.
int[][] scores = new int[5][]
Is it legal declaration?
Yes, it is legal to leave out the size for later dimensions of an array of arrays, the first one is required.
random() method return type
it returns double
round() method return type
returns an int when called with a float
returns a long when called with a double