Skip to content

Instantly share code, notes, and snippets.

//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());
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());
}
//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);
}
public class Person {
final String firstName;
final String lastName;
final String email; // This can be null
final String phone; //This can be null
}
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();
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];
listener, listenError := net.ListenTCP("tcp", addr)
if listenError != nil {
return nil, fmt.Errorf("Listen: %s", listenError)
}
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 {
public void close() {
try {
this.writer.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
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)