Oracle Certified Professional Java SE 8 Programmer
1 Java Class Design
1-1 Implement encapsulation
1-2 Implement inheritance including visibility modifiers and composition
1-3 Implement polymorphism
1-4 Override hashCode, equals, and toString methods from Object class
1-5 Create and use singleton classes and immutable classes
1-6 Develop code that uses static keyword on initializers, variables, methods, and classes
2 Advanced Java Class Design
2-1 Develop code that uses abstract classes and methods
2-2 Develop code that uses the final keyword
2-3 Create inner classes including static inner class, local class, nested class, and anonymous inner class
2-4 Use enumerated types including methods, and constructors in an enum type
2-5 Develop code that declares, implements andor extends interfaces and use the atOverride annotation
2-6 Create and use Lambda expressions
3 Generics and Collections
3-1 Create and use a generic class
3-2 Create and use ArrayList, TreeSet, TreeMap, and ArrayDeque objects
3-3 Use java util Comparator and java lang Comparable interfaces
3-4 Collections Streams and Filters
3-5 Iterate using forEach methods of Streams and List
3-6 Describe Stream interface and Stream pipeline
3-7 Use method references with Streams
4 Lambda Built-in Functional Interfaces
4-1 Use the built-in interfaces included in the java util function package such as Predicate, Consumer, Function, and Supplier
4-2 Develop code that uses primitive versions of functional interfaces
4-3 Develop code that uses binary versions of functional interfaces
4-4 Develop code that uses the UnaryOperator interface
5 Java Stream API
5-1 Develop code to extract data from an object using map, peek, and flatMap methods
5-2 Search for data by using search methods of the Stream classes including findFirst, findAny, anyMatch, allMatch, noneMatch
5-3 Develop code that uses the Optional class
5-4 Develop code that uses Stream data methods and calculation methods
5-5 Sort a collection using Stream API
5-6 Save results to a collection using the collect method and grouppartition data using the Collectors class
5-7 Use flatMap() methods in the Stream API
6 Exceptions and Assertions
6-1 Use try-catch and throw statements
6-2 Use catch, multi-catch, and finally clauses
6-3 Use Autoclose resources with a try-with-resources statement
6-4 Create custom exceptions and Auto-closeable resources
6-5 Test invariants by using assertions
7 Use Java SE 8 DateTime API
7-1 Create and manage date-based and time-based events including a combination of date and time into a single object using LocalDate, LocalTime, LocalDateTime, Instant, Period, and Duration
7-2 Work with dates and times across time zones and manage changes resulting from daylight savings including Format date and times values
7-3 Define and create and manage date-based and time-based events using Instant, Period, Duration, and TemporalUnit
8 Java File IO (NIO 2)
8-1 Operate on file and directory paths using the Paths class
8-2 Check, delete, copy, and move files and directories using the Files class
8-3 Recursively access a directory tree using the DirectoryStream and FileVisitor interfaces
8-4 Find a file by using the PathMatcher interface, and use Java SE 8 IO improvements, including Files find(), Files walk(), and lines() methods
8-5 Observe the changes in a directory by using WatchService
9 Java Concurrency
9-1 Create worker threads using Runnable, Callable and use an ExecutorService to concurrently execute tasks
9-2 Identify potential threading problems among deadlock, starvation, livelock, and race conditions
9-3 Use synchronized keyword and java util concurrent atomic package to control the order of thread execution
9-4 Use java util concurrent collections and classes including CyclicBarrier and CopyOnWriteArrayList
9-5 Use parallel ForkJoin Framework
9-6 Use parallel Streams including reduction, decomposition, merging processes, pipelines, and performance
10 Building Database Applications with JDBC
10-1 Describe the interfaces that make up the core of the JDBC API including the Driver, Connection, Statement, and ResultSet interfaces and their relationship to provider implementations
10-2 Identify the components required to connect to a database using the DriverManager class including the JDBC URL
10-3 Submit queries and read results from the database including creating statements, returning result sets, iterating through the results, and properly closing result sets, statements, and connections
10-4 Use PreparedStatement to perform CRUD operations
10-5 Use CallableStatement to call stored procedures
10-6 Use Transactions including disabling auto-commit mode, committing and rolling back transactions
10-7 Use JDBC batch operations
10-8 Create and use RowSet objects using RowSetProvider and RowSetFactory
11 Localization
11-1 Read and set the locale by using the Locale object
11-2 Create and manage date- and time-based events by using Localization including formatting dates, numbers, and currency values
11-3 Work with dates, numbers, and currency values by using the NumberFormat and DateFormat classes and their derived classes such as DecimalFormat and SimpleDateFormat
11-4 Build a user interface for a localized application
11-5 Describe the advantages of localizing an application
6 Exceptions and Assertions in Java

6 Exceptions and Assertions in Java

In Java, exceptions and assertions are crucial for handling errors and ensuring program correctness. This webpage will explore six key concepts related to exceptions and assertions, providing detailed explanations and examples to enhance your understanding.

Key Concepts

1. Checked Exceptions

Checked exceptions are exceptions that must be either caught or declared in the method signature using the throws keyword. They are checked at compile time and represent recoverable conditions.

Example

import java.io.FileReader;
import java.io.IOException;

public class CheckedExceptionExample {
    public static void main(String[] args) {
        try {
            FileReader file = new FileReader("file.txt");
        } catch (IOException e) {
            System.out.println("File not found or cannot be read.");
        }
    }
}
    

2. Unchecked Exceptions

Unchecked exceptions, also known as runtime exceptions, do not need to be declared or caught. They typically represent programming errors and are not checked at compile time.

Example

public class UncheckedExceptionExample {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3};
        System.out.println(numbers[5]); // Throws ArrayIndexOutOfBoundsException
    }
}
    

3. Custom Exceptions

Custom exceptions are user-defined exceptions that extend the Exception or RuntimeException class. They allow you to create specific exceptions tailored to your application's needs.

Example

class CustomException extends Exception {
    public CustomException(String message) {
        super(message);
    }
}

public class CustomExceptionExample {
    public static void main(String[] args) {
        try {
            throw new CustomException("This is a custom exception.");
        } catch (CustomException e) {
            System.out.println(e.getMessage());
        }
    }
}
    

4. Assertions

Assertions are used to test assumptions about the program. They are typically used during development and testing to catch logical errors early. Assertions can be enabled or disabled at runtime.

Example

public class AssertionExample {
    public static void main(String[] args) {
        int value = 10;
        assert value > 20 : "Value is not greater than 20";
        System.out.println("Value is greater than 20");
    }
}
    

5. Try-With-Resources

The try-with-resources statement is used to automatically close resources like files or network connections when they are no longer needed. It ensures that resources are properly managed and closed, even if an exception occurs.

Example

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryWithResourcesExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("Error reading file.");
        }
    }
}
    

6. Multi-Catch Blocks

Multi-catch blocks allow you to handle multiple exceptions in a single catch block. This reduces code duplication and makes exception handling more concise.

Example

public class MultiCatchExample {
    public static void main(String[] args) {
        try {
            int[] numbers = {1, 2, 3};
            System.out.println(numbers[5]); // Throws ArrayIndexOutOfBoundsException
            int result = 10 / 0; // Throws ArithmeticException
        } catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
    

Examples and Analogies

Checked Exceptions: Insurance Policy

Think of checked exceptions as an insurance policy that you must purchase before engaging in certain activities. You need to handle potential risks (exceptions) before proceeding.

Unchecked Exceptions: Traffic Violation

Consider unchecked exceptions as traffic violations that occur due to driver errors. These are not checked beforehand but can result in immediate consequences.

Custom Exceptions: Custom Rules

Custom exceptions are like custom rules in a game. They define specific conditions that, if violated, result in a custom penalty or notification.

Assertions: Quality Check

Assertions are like quality checks during manufacturing. They ensure that products meet certain standards before proceeding to the next stage.

Try-With-Resources: Automatic Cleanup

Try-with-resources is akin to an automatic dishwasher. It ensures that dishes (resources) are cleaned and put away properly, even if something goes wrong during the process.

Multi-Catch Blocks: Universal Remote

Multi-catch blocks are like a universal remote that can handle multiple devices. It simplifies the process of managing different devices (exceptions) with a single control.

Conclusion

Understanding and effectively using exceptions and assertions in Java is essential for writing robust and maintainable code. By mastering checked and unchecked exceptions, custom exceptions, assertions, try-with-resources, and multi-catch blocks, you can handle errors gracefully and ensure program correctness. This knowledge is crucial for passing the Oracle Certified Professional Java SE 8 Programmer exam.