Java SE 11 Fundamentals and API Usage - Comprehensive Guide (1Z0-819 Exam Topic 1)

Java SE 11 Fundamentals and API Usage - 1Z0-819 Exam Topic 1

This comprehensive guide covers the first exam topic for Oracle Certified Professional Java SE 11 Developer (1Z0-819), focusing on Java Fundamentals and API usage. This section comprises approximately 15% of the exam content and is essential for building a strong Java foundation.

1. Primitive Data Types and Wrapper Classes

Java has 8 primitive data types with specific characteristics:

Type Size Range Default Value Wrapper Class
byte 8 bits -128 to 127 0 Byte
short 16 bits -32,768 to 32,767 0 Short
int 32 bits -2³¹ to 2³¹-1 0 Integer
long 64 bits -2⁶³ to 2⁶³-1 0L Long
float 32 bits ±1.4E-45 to ±3.4E38 0.0f Float
double 64 bits ±4.9E-324 to ±1.7E308 0.0d Double
char 16 bits ‘\u0000’ to ‘\uffff’ ‘\u0000’ Character
boolean 1 bit true/false false Boolean

Autoboxing and Unboxing

Java automatically converts between primitives and their wrapper classes:

1
2
3
4
5
6
7
// Autoboxing: primitive → wrapper
Integer a = 10; // Compiler converts to Integer.valueOf(10)
Double b = 3.14; // Compiler converts to Double.valueOf(3.14)

// Unboxing: wrapper → primitive
int c = a; // Compiler converts to a.intValue()
double d = b; // Compiler converts to b.doubleValue()

Integer Cache Behavior

Java caches Integer values from -128 to 127 for performance:

1
2
3
4
5
6
7
Integer x = 127;
Integer y = 127;
System.out.println(x == y); // true (cached objects)

Integer m = 128;
Integer n = 128;
System.out.println(m == n); // false (new objects)

Best Practices

  • Use primitives for performance-sensitive code
  • Use wrapper classes with generics (e.g., List<Integer>)
  • Prefer valueOf() over constructors for wrapper objects
  • Handle null carefully to avoid NullPointerExceptions

2. String Handling and Manipulation

Strings are immutable objects with key methods:

String Creation

1
2
3
4
5
6
7
// String pool vs heap allocation
String s1 = "Java"; // String pool
String s2 = new String("Java"); // Heap memory
String s3 = s1.intern(); // Returns canonical representation

System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // true

String Methods (Java 11+)

1
2
3
4
5
6
7
8
9
10
11
12
String text = "  Java SE 11  ";

// New Java 11 methods
System.out.println(text.strip()); // "Java SE 11"
System.out.println(text.repeat(2)); // " Java SE 11 Java SE 11 "
System.out.println(" ".isBlank()); // true
System.out.println("Java\nSE\n11".lines().count()); // 3

// Traditional methods
System.out.println(text.trim()); // "Java SE 11"
System.out.println(text.substring(2, 6)); // "Java"
System.out.println(String.join("-", "A", "B", "C")); // "A-B-C"

Text Blocks (Java 15+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// HTML example
String html = """
<html>
<body>
<p>Hello %s</p>
</body>
</html>""".formatted("World");

// JSON example
String json = """
{
"name": "John",
"age": 30,
"languages": ["Java", "Python"]
}""";

String Building

1
2
3
4
5
6
// StringBuilder for mutable operations
StringBuilder sb = new StringBuilder("Java");
sb.append(" SE").append(" 11");
sb.insert(0, "Oracle ");
sb.delete(0, 7);
System.out.println(sb.toString()); // "Java SE 11"

3. Proper Usage of Optional Class

Optional helps avoid NullPointerExceptions and makes null checks explicit:

Creation and Access

1
2
3
4
5
6
7
8
9
10
11
12
// Creating Optionals
Optional<String> empty = Optional.empty();
Optional<String> name = Optional.of("Alice"); // Throws NPE if null
Optional<String> maybeName = Optional.ofNullable(getName()); // Safe

// Accessing values
String value = maybeName.orElse("default");
String value2 = maybeName.orElseGet(() -> generateDefault());
String value3 = maybeName.orElseThrow(IllegalStateException::new);

// Conditional actions
maybeName.ifPresent(System.out::println);

Chaining Operations

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Optional<User> user = findUserById(123);

// Chaining with map, filter, flatMap
String email = user
.filter(u -> u.getAge() > 18)
.flatMap(User::getEmail) // Returns Optional<String>
.map(String::toUpperCase)
.orElse("N/A");

// vs nested null checks
User u = findUserById(123);
String email2 = "N/A";
if (u != null && u.getAge() > 18) {
String e = u.getEmail();
if (e != null) {
email2 = e.toUpperCase();
}
}

Best Practices

  • Avoid using Optional for:
    • Class fields
    • Method parameters
    • Collection elements
  • Never call get() without checking isPresent()
  • Prefer orElseGet() for expensive default operations
  • Use orElseThrow() for required values

4. Date and Time API (java.time package)

Modern date/time handling replaces outdated Date/Calendar classes:

Core Classes

1
2
3
4
5
6
7
8
9
10
11
// Dates
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1990, Month.JANUARY, 1);

// Times
LocalTime now = LocalTime.now();
LocalTime meetingTime = LocalTime.of(14, 30); // 2:30 PM

// Date-Time
LocalDateTime current = LocalDateTime.now();
ZonedDateTime zoned = ZonedDateTime.now(ZoneId.of("Asia/Taipei"));

Periods and Durations

1
2
3
4
5
6
7
8
// Period: date-based amount
Period age = Period.between(birthday, today);
System.out.printf("Age: %d years, %d months%n",
age.getYears(), age.getMonths());

// Duration: time-based amount
Duration meetingLength = Duration.ofHours(1).plusMinutes(30);
LocalTime endTime = meetingTime.plus(meetingLength);

Formatting and Parsing

1
2
3
4
5
6
7
8
9
// Formatting
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("dd/MM/yyyy HH:mm:ss")
.withLocale(Locale.US);

String formatted = current.format(formatter);

// Parsing
LocalDateTime parsed = LocalDateTime.parse("20/06/2025 14:30:00", formatter);

Time Zones

1
2
3
4
5
6
7
8
// Time zone conversions
ZonedDateTime nyTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
ZonedDateTime twTime = nyTime.withZoneSameInstant(ZoneId.of("Asia/Taipei"));

// Daylight Saving Time handling
ZoneId.getAvailableZoneIds().stream()
.filter(z -> z.contains("America"))
.forEach(System.out::println);

5. Control Flow Statements and Pattern Matching

Modern Java features for more expressive control flow:

Enhanced Switch (Java 14+)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int dayOfWeek = 3;
String dayType = switch (dayOfWeek) {
case 1, 2, 3, 4, 5 -> {
System.out.println("Weekday");
yield "Weekday";
}
case 6, 7 -> "Weekend";
default -> throw new IllegalArgumentException("Invalid day: " + dayOfWeek);
};

// Switch expressions return values
String dayName = switch (dayOfWeek) {
case 1 -> "Monday";
case 2 -> "Tuesday";
// ...
default -> "Unknown";
};

Pattern Matching (Java 16+)

1
2
3
4
5
6
7
8
9
10
11
12
13
// instanceof pattern matching
Object obj = "Hello Java 11";
if (obj instanceof String s && s.length() > 5) {
System.out.println(s.toUpperCase());
}

// Pattern matching in switch (Java 21 preview)
String formatted = switch (obj) {
case Integer i -> String.format("int %d", i);
case String s -> String.format("String %s", s);
case null -> "null";
default -> obj.toString();
};

Traditional Control Flow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// For-each loop
List<String> languages = List.of("Java", "Python", "C++");
for (String lang : languages) {
System.out.println(lang);
}

// Try-with-resources
try (InputStream is = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}

Summary and Exam Tips

1. Identifying Correct Output of Code Snippets

Expect questions that show code snippets and ask for the output. Focus on:

Key areas:

  • Autoboxing/unboxing behavior
1
2
3
4
Integer a = 1000, b = 1000;
System.out.println(a == b); // false (outside cache range)
Integer c = 100, d = 100;
System.out.println(c == d); // true (within -128 to 127 cache)
  • String manipulation results
1
2
3
String s = "Java";
s.concat(" SE 11");
System.out.println(s); // "Java" (strings are immutable)
  • Date/time calculations
1
2
LocalDate date = LocalDate.of(2023, 2, 28);
System.out.println(date.plusMonths(1)); // 2023-03-28

2. Spotting Potential NullPointerExceptions

Identify code that could throw NPEs:

Common pitfalls:

  • Unboxing null wrapper objects
1
2
Optional<Integer> opt = Optional.empty();
int num = opt.orElse(null); // NPE when unboxing null
  • Method chaining on potentially null objects
1
String name = findUser().getName(); // NPE if findUser() returns null
  • Accessing array elements before initialization
1
2
int[] numbers = null;
System.out.println(numbers[0]); // NPE

3. Recognizing Proper API Usage

Identify correct vs incorrect API usage:

Key verification points:

  • Proper Optional handling
1
2
3
4
5
6
7
8
// Correct
Optional<String> name = Optional.ofNullable(getName());
name.ifPresent(System.out::println);

// Incorrect
if (name.isPresent()) {
System.out.println(name.get()); // Avoid .get() without check
}
  • Valid date/time operations
1
2
3
4
5
// Correct
LocalDate date = LocalDate.parse("2023-01-01");

// Incorrect
Date legacyDate = new Date("2023-01-01"); // Deprecated constructor
  • Appropriate string methods
1
2
3
4
5
// Correct
" text ".strip();

// Incorrect for Unicode spaces
" text ".trim(); // Doesn't remove all Unicode whitespace

4. Understanding Performance Implications

Analyze code for performance issues:

Critical areas:

  • Autoboxing in loops
1
2
3
4
5
// High overhead
Long sum = 0L;
for (long i = 0; i < Integer.MAX_VALUE; i++) {
sum += i; // Creates new Long object each iteration
}
  • String concatenation in loops
1
2
3
4
5
6
7
8
9
10
11
// Inefficient
String result = "";
for (int i = 0; i < 1000; i++) {
result += i; // Creates new String each iteration
}

// Efficient
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
sb.append(i);
}
  • Optional method costs
1
2
3
4
5
// Expensive default
String value = optional.orElse(createExpensiveDefault());

// Better
String value = optional.orElseGet(() -> createExpensiveDefault());

Exam Strategy

  1. Read questions carefully - note keywords like “correct”, “best”, “most efficient”
  2. Eliminate obviously wrong answers first
  3. For code output questions, trace execution step-by-step
  4. Watch for trick questions with:
    • == vs equals() comparisons
    • Method chaining on null references
    • Mutable vs immutable objects
    • Time zone conversions
  5. Practice with sample questions from:
    • Enthuware Java 11 Practice Tests
    • Oracle’s official sample questions
    • Whizlabs 1Z0-819 practice exams

2. String Handling and Manipulation

Strings are immutable objects with key methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
String str = "Java 11";
str.length(); // 7
str.substring(0,4); // "Java"
str.contains("11"); // true
str.repeat(2); // "Java 11Java 11" (Java 11 feature)

// String joining
String joined = String.join("-", "A", "B", "C"); // "A-B-C"

// Text blocks (Java 15+)
String html = """
<html>
<body>
<p>Hello World</p>
</body>
</html>""";

3. Proper Usage of Optional Class

Optional helps avoid NullPointerExceptions:

1
2
3
4
5
6
7
8
9
10
11
12
13
Optional<String> optional = Optional.ofNullable(getName());
String name = optional.orElse("default");

// Chaining operations
String result = optional
.filter(s -> s.length() > 3)
.map(String::toUpperCase)
.orElse("N/A");

// Best practices:
// - Avoid Optional for class fields
// - Don't use Optional in collections
// - Prefer orElseGet() for expensive defaults

4. Date and Time API (java.time package)

Modern date/time handling:

1
2
3
4
5
6
7
8
9
10
11
12
13
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1990, Month.JANUARY, 1);

Period age = Period.between(birthday, today);
System.out.println(age.getYears()); // Age in years

// Time manipulation
LocalTime time = LocalTime.of(14, 30);
LocalTime later = time.plusHours(2); // 16:30

// Formatting
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatted = today.format(formatter);

5. Control Flow Statements and Pattern Matching

Traditional control flow:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Enhanced switch (Java 14+)
int day = 3;
String dayType = switch (day) {
case 1, 2, 3, 4, 5 -> "Weekday";
case 6, 7 -> "Weekend";
default -> "Invalid";
};

// Pattern matching (Java 16+)
Object obj = "Hello";
if (obj instanceof String s) {
System.out.println(s.length()); // s is automatically cast
}

Summary

Key exam focus areas:

  • Understand autoboxing/unboxing behavior
  • Master String manipulation methods
  • Apply Optional correctly to avoid NPEs
  • Use java.time API for date/time operations
  • Utilize modern control flow features

For exam preparation, practice writing code that demonstrates these concepts and understand the performance implications of each approach.

Java SE 11 Fundamentals and API Usage - Comprehensive Guide (1Z0-819 Exam Topic 1)

https://blog.kwunlam.com/Java-SE-11-Fundamentals-and-API-Usage-1Z0-819-Exam-Topic-1/

Author

Elliot

Posted on

2025-06-20

Updated on

2025-06-19

Licensed under