10
Mengapa menggunakan Opsional di Java 8+ alih-alih pemeriksaan pointer nol tradisional?
Kami baru saja pindah ke Java 8. Sekarang, saya melihat aplikasi dibanjiri Optionalobjek. Sebelum Java 8 (Gaya 1) Employee employee = employeeServive.getEmployee(); if(employee!=null){ System.out.println(employee.getId()); } After Java 8 (Gaya 2) Optional<Employee> employeeOptional = Optional.ofNullable(employeeService.getEmployee()); if(employeeOptional.isPresent()){ Employee employee = employeeOptional.get(); System.out.println(employee.getId()); } Saya tidak melihat nilai tambah Optional<Employee> employeeOptional = employeeService.getEmployee();ketika …