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
/** 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); |
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
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(), |
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
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; |
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
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; |
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
// 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(); |
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
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) |
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
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() |
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
// 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) { |
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
/** | |
* 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 | |
* |
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
abstract class Brick { | |
public Brick() { | |
greet(); | |
} | |
public abstract void greet(); | |
} | |
public class HardPlace { | |
public class Between extends Brick { |
NewerOlder