This file contains 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 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 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 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 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 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 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) | |
} |
This file contains 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
def main(args: Array[String]): Unit = { | |
val res = divide(10, 0) | |
res match { | |
case Left(value) => println(s"Result is ${value}") | |
case Right(e) => println(s"Calculation failed reason ${e}") | |
} | |
val result = trydivide(10, 0) | |
result match { |
This file contains 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 void close() { | |
try { | |
this.writer.close(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} |
This file contains 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
int main () | |
{ | |
FILE * fp; | |
fp = fopen ("somejunkfile.txt", "rb"); | |
if (fp == NULL) | |
{ | |
printf("errno: %d\n", errno); | |
printf("Error opening the file: %s\n", strerror(errno)); | |
exit(EXIT_FAILURE) |