Skip to content

Instantly share code, notes, and snippets.

/** In memory of @crazybob, who loved puzzles like this. */
public class OverridingFinalToStringPuzzle {
private static class Bull {
public final String toString() {
return "Bull";
}
}
public static String convert(Bull bull) {
return "%s".formatted(bull);
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class StreamMapInvertingCompilerBug {
public static void main(String... args) {
Map<Integer, Long> numberCount =
Stream.of(1, 4, 2, 5, 3, 5, 1, 3, 5, 3, 2, 1, 4, 5)
.collect(Collectors.groupingBy(
Function.identity(),
package richardstartin;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
package richardstartin;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
// run with java -showversion CleanerDemo in Java 16 and 17 to see different results
import java.lang.ref.*;
public class CleanerDemo {
public static void main(String... args) throws InterruptedException {
Cleaner cleaner = Cleaner.create();
Cleaner.Cleanable cleanable = cleaner.register(new Object(),
() -> System.out.println("My cleaner thread priority is " +
Thread.currentThread().getPriority()));
System.gc();
package benchmarks;
import org.openjdk.jmh.annotations.*;
import java.util.*;
import java.util.concurrent.*;
@Fork(value = 1)
@Warmup(iterations = 5, time = 3)
@Measurement(iterations = 10, time = 3)
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.thread
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
fun atomicBoolean(default: Boolean = false) =
object : ReadWriteProperty<Any?, Boolean> {
val storage = AtomicBoolean(default)
override fun getValue(thisRef: Any?, property: KProperty<*>) =
storage.get()
@kabutz
kabutz / Java9AtLast.java
Created September 23, 2017 08:42
Three cheers (or more) for Java 9 release!
// Compile and run in Java 9. Then compile and run in Java 8 and 9. Enjoy your new JDK :-D
public class Java9AtLast {
public static void hiphip(int cheers) {
try {
hiphip(cheers + 1);
} catch(StackOverflowError er) {
System.out.println("hooray x " + cheers);
}
}
public static void main(String... args) {
@kabutz
kabutz / One.java
Created November 4, 2016 20:55
Strange class that compiled with JDK 9, but not 1.6, 1.7 nor 1.8.
/**
* As seen on https://javaspecialists.slack.com/messages/general
*
* This class fails to compile with JDK 1.6, 1.7 and 1.8:
*
* class Two extends Observable { // Nothing special about the choice of Observable here
* ^
* symbol: class Observable
* 1 error
*
@kabutz
kabutz / virtualproxy.swift
Created October 12, 2016 15:49
Example of how easily we can build a "virtual proxy" in Swift 3.0
/*
All it takes is the "lazy" keyword in front of the var definition and the object is instantiated when it is first used. Brilliant.
*/
class MoralFibre {
init() {
print("***Creating MoralFibre***")
}
func actSociallyResponsibly() -> Int {
print("supporting local soup kitchen")