This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public Optional<String> getEmail() { | |
| return Optional.ofNullable(email); | |
| } | |
| public Optional<String> getPhone() { | |
| return Optional.ofNullable(phone); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Address has priority , first home and then Office | |
| if (p.home != null) { | |
| System.out.println("Contacted at home address " + p.home.address); | |
| return; // Magical return for early exit | |
| } | |
| if (p.office != null) { | |
| System.out.println("Contacted at office address " + p.office.address); | |
| return; // Magical return for early exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Nested Property | |
| if (p.getHome() != null) { | |
| System.out.println("Sending Postal mail " + p.getHome().address); | |
| } | |
| if (p.getHome() != null && p.getHome().getInsurance() != null) { | |
| System.out.println("Sending Notification to insurance " + p.getHome().getInsurance().getAgency()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Always Happy | |
| Optional<String> phone = contactNumber(p); | |
| Optional<String> email = email(p); | |
| System.out.println("Calling Phone " + phone.get()); | |
| System.out.println("Sending Email " + email.get()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Optional<String> phone = contactNumber(p); | |
| Optional<String> email = email(p); | |
| if (phone.isPresent()) { | |
| System.out.println("Calling Phone " + phone.get()); | |
| } | |
| if (email.isPresent()) { | |
| System.out.println("Sending Email " + email.get()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Not using optional | |
| if (p.email != null) { | |
| System.out.println("Sending email to " + p.email); | |
| } | |
| if (p.phone != null) { | |
| System.out.println("Calling " + p.phone); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Person { | |
| final String firstName; | |
| final String lastName; | |
| final String email; // This can be null | |
| final String phone; //This can be null | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class VerifyCurrentGC { | |
| public static void main(String... args) { | |
| var gcBeans = ManagementFactory.getGarbageCollectorMXBeans(); | |
| gcBeans.stream().forEach(gc -> { | |
| out.println(format("GC Name : %s", gc.getName())); | |
| var poolNames = gc.getMemoryPoolNames(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MemoryAllocator { | |
| public static final int KB = 1024; | |
| static int mbToAllocate = Integer.getInteger("mb", 1000); | |
| public static void main(String[] args) { | |
| System.out.println(String.format("Start allocation of %s MBs", mbToAllocate)); | |
| for (var i = 0; i < mbToAllocate; i++) { | |
| var garbage = new byte[KB * KB]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| listener, listenError := net.ListenTCP("tcp", addr) | |
| if listenError != nil { | |
| return nil, fmt.Errorf("Listen: %s", listenError) | |
| } |