Java Puzzlers NG S04 - Looks like with this release cycle the puzzlers are here to stay

A presentation at DevNexus 2019 in March 2019 in Atlanta, GA, USA by Baruch Sadogursky

Slide 1

Slide 1

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 2

Slide 2

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 3

Slide 3

Baruch Sadogursky, Head of Developer Relations, @jfrog @jbaruch Viktor Gamov, Developer Advocate, @confluent @gamussa @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 4

Slide 4

-Shownotes! -jfrog.com/shownotes -Slides -Video (soon) -Links -Comments, ratings, raffle @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 5

Slide 5

Two funny folks on stage 2. Funny and educating puzzlers 3. You vote! (that’s important) 4. We throw t-shirts on you (sponsored by confuent, thank you!) 5. official: #javapuzzlersng #devnexus @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 6

Slide 6

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 7

Slide 7

Voting system check: what java are you running? A. B. C. D. E. F. Java 8 Java 12 Java 11 Java 9 Java 5 Java 2 @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 8

Slide 8

Java puzzlers are like: @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 9

Slide 9

Everything works (or doesn’t work) with the latest openjdk release! @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 10

Slide 10

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 11

Slide 11

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 12

Slide 12

class This { final String thіs; This() { this.thіs = This.this.thіs + “this”; } } public static void main(String[] args) { System.out.println(new This().thіs.length()); } class This { final String thіs; This() { this.thіs = this.thіs + “this”; } } A. First compiles, second does not B. Second compiles, first does not C. Both compile D. Stop drugs!! public static void main(String[] args) { System.out.println(new This().thіs.length()); } @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 13

Slide 13

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 14

Slide 14

class This { final String thіs; This() { this.thіs = This.this.thіs + “this”; } } public static void main(String[] args) { System.out.println(new This().thіs.length()); } class This { final String thіs; This() { this.thіs = this.thіs + “this”; } } A. First compiles, second does not B. Second compiles, first does not C. Both compile D. Stop drugs!! public static void main(String[] args) { System.out.println(new This().thіs.length()); } @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 15

Slide 15

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 16

Slide 16

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 17

Slide 17

class This { final String thіs; This() { this.thіs = This.this.thіs + “this”; } } public static void main(String[] args) { System.out.println(new This().thіs.length()); } A. Runtime Exception B. 4 C. 8 D. Won’t compile (yes, I am a slowpoke) @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 18

Slide 18

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 19

Slide 19

class This { final String thіs; This() { this.thіs = This.this.thіs + “this”; } } public static void main(String[] args) { System.out.println(new This().thіs.length()); } A. Runtime Exception B. 4 C. 8 D. Won’t compile (yes, I am a slowpoke) @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 20

Slide 20

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 21

Slide 21

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 22

Slide 22

How we understand bitcoin/blockchain hype !Something is spinning/mining !High CPU load !High heat omission !Someone made a lot of money once !That’s all we know @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 23

Slide 23

What won’t compile? A. B. C. D. for for {;} for ; for (;;) (;;) (;;) (;;) {;;} ;; {;} ; E. All good, folks! @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 24

Slide 24

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 25

Slide 25

What won’t compile? A. B. C. D. for for {;} for ; for (;;) (;;) (;;) (;;) {;;} ;; {;} ; E. All good, folks! @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 26

Slide 26

What won’t compile? A. B. C. D. for for {;} for ; for (;;) (;;) (;;) (;;) {;;} ;; {;} ; Unreachable statement E. All good, folks! @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 27

Slide 27

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 28

Slide 28

public static void main(String[] args) { final boolean rick; } if(true && (rick = true)) {} if(false && (rick = false)) {} System.out.println(rick); public static void main(String[] args) { final boolean rick; } if(false && (rick = false)) {} if(true && (rick = true)) {} System.out.println(rick); @jbaruch @gamussa #javapuzzlersng A. First compiles, second does not B. Second compiles, first does not C. Both compile D.Stop drugs!! #devnexus jfrog.com/shownotes

Slide 29

Slide 29

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 30

Slide 30

public static void main(String[] args) { final boolean rick; } if(true && (rick = true)) {} if(false && (rick = false)) {} System.out.println(rick); public static void main(String[] args) { final boolean rick; } if(false && (rick = false)) {} if(true && (rick = true)) {} System.out.println(rick); @jbaruch @gamussa #javapuzzlersng A. First compiles, second does not B. Second compiles, first does not C. Both compile D.Stop drugs!! #devnexus jfrog.com/shownotes

Slide 31

Slide 31

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 32

Slide 32

entry public static void main(String[] args) { final boolean rick; } if(true && (rick = true)) {} if(false && (rick = false)) {} System.out.println(rick); true rick = true false rick = false println @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 33

Slide 33

entry false rick = false public static void main(String[] args) { final boolean rick; } if(false && (rick = false)) {} if(true && (rick = true)) {} System.out.println(rick); true rick = true println @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 34

Slide 34

entry false public static void main(String[] args) { final boolean rick; } if(false && (rick = false)) {} if(true && (rick = true)) {} System.out.println(rick); true rick = true println @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 35

Slide 35

rick = false public static void main(String[] args) { final boolean rick; } if(false && (rick = false)) {} if(true && (rick = true)) {} System.out.println(rick); true rick = true println @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 36

Slide 36

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 37

Slide 37

public static void main(String[] args) { System.out.println(carOrder(” fuchsia”)); } static <Color> Color carOrder(Color color) { class ModelT extends RuntimeException { Color color; ModelT(Color color) { this.color = color; // 1 } } } if (color.equals(0x000000)) throw new ModelT(color); // 2 try { carOrder(0x000000); A.Won’t compile } catch (ModelT car) { color = car.color; // 3 B.Empty line } C.0 return color; D.ClassCastException @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 38

Slide 38

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 39

Slide 39

public static void main(String[] args) { System.out.println(carOrder(” fuchsia”)); } static <Color> Color carOrder(Color color) { class ModelT extends RuntimeException { Color color; ModelT(Color color) { this.color = color; // 1 } } } if (color.equals(0x000000)) throw new ModelT(color); // 2 try { carOrder(0x000000); A.Won’t compile } catch (ModelT car) { color = car.color; // 3 B.Empty line } C.0 return color; D.ClassCastException @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 40

Slide 40

public static void main(String[] args) { System.out.println(carOrder(” fuchsia”)); } static <Color> Color carOrder(Color color) { class ModelT extends RuntimeException { Color color; ModelT(Color color) { this.color = color; // 1 } } } if (color.equals(0x000000)) throw new ModelT(color); // 2 try { carOrder(0x000000); Unchecked warning: } catch (ModelT car) { A.//1 color = car.color; // 3 } B.//2 return color; @jbaruch @gamussa C.//3 D.Not gonna happen #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 41

Slide 41

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 42

Slide 42

public static void main(String[] args) { System.out.println(carOrder(” fuchsia”)); } static <Color> Color carOrder(Color color) { class ModelT extends RuntimeException { Color color; ModelT(Color color) { this.color = color; // 1 } } } if (color.equals(0x000000)) throw new ModelT(color); // 2 try { carOrder(0x000000); Unchecked warning: } catch (ModelT car) { A.//1 color = car.color; // 3 } B.//2 return color; @jbaruch @gamussa C.//3 D.Not gonna happen #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 43

Slide 43

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 44

Slide 44

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 45

Slide 45

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 46

Slide 46

public class Main { public static void main(String[] args) throws InterruptedException { new Object().wait(9223372036854775807L, 1); } } A.Long wait ahead B.Fails with IllegalMonitorStateException C. Fails with IllegalArgumentException D.Fails with InterruptedException @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 47

Slide 47

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 48

Slide 48

public class Main { public static void main(String[] args) throws InterruptedException { new Object().wait(9223372036854775807L, 1); } } A.Long wait ahead B.Fails with IllegalMonitorStateException C. Fails with IllegalArgumentException D.Fails with InterruptedException @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 49

Slide 49

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 50

Slide 50

WTF, Java(script?) @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 51

Slide 51

What will print “true”? private static void doubleWtf(Double wtf1, Double wtf2) { if (!wtf1.equals(wtf2)) { DoubleBuffer dWtf1 = DoubleBuffer.allocate(1).put(wtf1); DoubleBuffer dWtf2 = DoubleBuffer.allocate(1).put(wtf2); } System.out.println(dWtf1.equals(dWtf2)); } A. B. C. D. E. @jbaruch Double.NaN and Double.NaN Double.NaN and any double 0.0 and -0.0 Any double and a null Nothing @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 52

Slide 52

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 53

Slide 53

What will print “true”? private static void doubleWtf(Double wtf1, Double wtf2) { if (!wtf1.equals(wtf2)) { DoubleBuffer dWtf1 = DoubleBuffer.allocate(1).put(wtf1); DoubleBuffer dWtf2 = DoubleBuffer.allocate(1).put(wtf2); } System.out.println(dWtf1.equals(dWtf2)); } A. B. C. D. E. @jbaruch Double.NaN and Double.NaN Double.NaN and any double 0.0 and -0.0 Any double and a null Nothing @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 54

Slide 54

RTFM @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 55

Slide 55

Var of thrones @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 56

Slide 56

final var targaryens = 2; final var lannisters = 25; final var starks = ‘1’; final var snow = true ? targaryens * lannisters : starks; System.out.println(snow); A. 2 B. 25 C. 1 D. 50 @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 57

Slide 57

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 58

Slide 58

final var targaryens = 2; final var lannisters = 25; final var starks = ‘1’; final var snow = true ? targaryens * lannisters : starks; System.out.println(snow); A. 2 B. 25 C. 1 D. 50 @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 59

Slide 59

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 60

Slide 60

final var targaryens = 2; final var lannisters = 25; final var starks = ‘1’; final var snow = true ? targaryens * lannisters : starks; System.out.println(snow); A. 2 B. 25 C. 1 D. 50 @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 61

Slide 61

final int targaryens = 2; final int lannisters = 25; final char starks = ‘1’; final char snow = true ? targaryens * lannisters : starks; System.out.println(snow); A. 2 B. 25 C. 1 D. 50 @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 62

Slide 62

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 63

Slide 63

@jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 64

Slide 64

-Write readable code! -Comment all the tricks -Sometimes it’s just a bug -Static code analysis FTW IntelliJ IDEA! -RTFM @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 65

Slide 65

-You get it, more releases – more puzzlers! -Puzzlers? Gotchas? Fetal position inducing behavior? -puzzlers+java@jfrog.com @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 66

Slide 66

-Shownotes! -jfrog.com/shownotes -Slides -Video (soon) -Links -Comments, ratings, raffle @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes

Slide 67

Slide 67

-Did you like it? -Praise us on twitter! -# javapuzzlersng -# OracleCodeOne -@ jbaruch -@ gamussa -Didn’t like it? -/dev/null @jbaruch @gamussa #javapuzzlersng #devnexus jfrog.com/shownotes