Java puzzlers ng The strange, the bizarre, and the wonderful @ jbaruch
voxxeddayssingapore
javapuzzlersng
A presentation at Voxxed Days Singapore 2018 in June 2018 in Singapore by Baruch Sadogursky
Java puzzlers ng The strange, the bizarre, and the wonderful @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch @ gamussa
javapuzzlersng #devnexus2018
Baruch “Top Hat” Sadogursky, JFrog Developer Advocate, @ jbaruch @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ tagir_valeev @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch @ gamussa
shirts are airborne 5. Official twitter hashtag
javapuzzlersng
@ jbaruch @ gamussa
Raffle! Shownotes !
@ jbaruch @ gamussa
javapuzzlersng #devnexus2018
Which Java version are you on? @ jbaruch
voxxeddayssingapore
javapuzzlersng A. Java 7 B. Java 8 C. Java 9 D. Java 5 E. Java 10 F. Java 2
Watching the puzzlers like … #dafaq @ jbaruch
voxxeddayssingapore
javapuzzlersng
Everything works (or doesn't) in the latest Java 8 and/or 9 update
Execute ’ em all
{ Files. write ( Paths. get ( " Sentence.txt " ), sentence); }); // 2 }
Semicolons are evil! @ jbaruch
voxxeddayssingapore
javapuzzlersng
{ Files. write ( Paths. get ( " Sentence.txt " ), sentence); }); // 2 }
{ Files. write ( Paths. get ( " Sentence.txt " ), sentence); }); // 2 } @ FunctionalInterface public interface Runnable { public abstract void run(); } @ FunctionalInterface public interface Callable< V { V call() throws Exception; } @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
What will happen? @ jbaruch
voxxeddayssingapore
{ if ( x.equals ( "Chuck" )) { list.remove (x); } });
@ jbaruch
voxxeddayssingapore
javapuzzlersng
Java 8 vs Chuck Norris @ jbaruch
voxxeddayssingapore
javapuzzlersng
{ if ( x.equals ( "Chuck" )) { list.remove (x); } }); @ jbaruch
voxxeddayssingapore
javapuzzlersng
Here’s why: stream(). forEach () à spliterator (). forEachRemaining () forEachRemaining checks for mod count once, in the end Removing element adds null to the end of the array : ["Arne", "Chuck", "Slay"] à ["Arne", "Slay", null] On the last iteration if( null.equals ("Chuck")) fails with NPE (didn’t get to CME ) Use list.removeIf ("Chuck"::equals); @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
{ if ( heroes .hasNext ()) { heroes .next (); heroes .remove (); } }); System. out .println (expendables); }
@ jbaruch
voxxeddayssingapore
javapuzzlersng
{ if ( heroes .hasNext ()) { heroes .next (); heroes .remove (); } }); System. out .println (expendables); }
Don’t do that. Really, don’t. killThemAll ( new ArrayList <String>( Arrays.asList ( "N" , "S" , "W" , "S" , "L" , "S" , "L" , "V" ))); [] killThemAll ( new LinkedList <String>( Arrays. asList ( "N" , "S" , "W" , "S" , "L" , "S" , "L" , "V" ))); [ S , S , S , V ] killThemAll ( new ArrayDeque <String>( Arrays. asList ( "N" , "S" , "W" , "S" , "L" , "S" , "L" , "V" ))); [ N,S,W,S,L,S,L,V ] killThemAll ( new TreeSet <String>( Arrays. asList ( "N" , "S" , "W" , "S" , "L" , "S" , "L" , "V" ))); [ N,W,L,L ] @ jbaruch
voxxeddayssingapore
javapuzzlersng
How single is a Single Abstract Method Interface? @ jbaruch
voxxeddayssingapore
javapuzzlersng
A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard (T), remove it and it will work C. Problem is the drinkIn methods, removing one of them and it will work D. It will work fine! Both partyHard () and drinkIn () are merged in SingleAndHappy , leaving one abstract method public interface Single< T
{ default void partyHard (String songName ) { System. out .println ( songName ); } void partyHard ( T songName ); void drinkIn ( T drinkName ); void drinkIn (String dringName ); } @ FunctionalInterface public interface SingleAndHappy extends Single<String> { } @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
A. WTF?! ’Single’ means one, not three! B. Problem is with partyHard (T), remove it and it will work C. Problem are the drinkIn methods, removing it will leave one abstract method D. Yes! Both partyHard () and drinkIn () are merged in SingleAndHappy , leaving one abstract method public interface Single< T
{ default void partyHard (String songName ) { System. out .println ( songName ); } void partyHard ( T songName ); void drinkIn ( T drinkName ); void drinkIn (String dringName ); } @ FunctionalInterface public interface SingleAndHappy extends Single<String> { } @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
info.java
module module {
requires requires;
exports exports;
opens opens;
uses uses;
}
A.
Error in every line
B.
Can’t export package with
exports
name
C.
Can’t declare uses in uses
D.
Compiles fine
@ jbaruch
voxxeddayssingapore
info.java
module module {
requires requires;
exports exports;
opens opens;
uses uses;
}
info.java
module module {
requires requires;
exports exports;
opens opens;
uses uses;
}
@
jbaruch
voxxeddayssingapore
javapuzzlersng
info.java
import
opens.uses
;
module module {
requires requires;
exports exports;
opens opens;
uses uses;
}
@
jbaruch
voxxeddayssingapore
javapuzzlersng
System.out.println ( isUltimateQuestion ? 42 : null); @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
System.out.println ( isUltimateQuestion ? 42 : isUltimateQuestion ? 42 : null); @ jbaruch
voxxeddayssingapore
javapuzzlersng
isUltimateQuestion = false A. Null B. Won’t compile C. 42 D. NullPointerException System.out.println ( isUltimateQuestion ? 42 : isUltimateQuestion ? 42 : null); @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
System.out.println ( isUltimateQuestion ? 42 : isUltimateQuestion ? 42 : null); isUltimateQuestion = false A. Null B. Won’t compile C. 42 D. NullPointerException @ jbaruch
voxxeddayssingapore
javapuzzlersng
15.25 : @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng What’s type of (b ? 42 : null) ? isUltimateQuestion ? 42 : null isUltimateQuestion ? 42 : isUltimateQuestion ? 42 : null int null Integer int Integer int Unboxing of null à NPE
@ jbaruch
voxxeddayssingapore
javapuzzlersng
public class Superhero {
private Long strength;
public Superhero(Long strength) {
//null check
here
this.strength
= strength;
}
public Long
getStrength
() {
return strength;
}
}
@
jbaruch
voxxeddayssingapore
javapuzzlersng
What’s the correct answer? class Superhero implements Comparable<Superhero> { … @Override public int compareTo (Superhero this, Superhero that) { return this.strength.compareTo ( that.strength ); } } class Superhero implements Comparable<Superhero> { … public int compareTo (Superhero me, Superhero you) { return me.strength.compareTo ( you.strength ); } } class Superhero implements Comparator<Superhero> { … @Override public int compare(Superhero batman, Superhero superman) return batman.strength.compareTo ( superman.strength ); } } D. None ! A B C @ jbaruch
voxxeddayssingapore
javapuzzlersng
What’s the correct answer? class Superhero implements Comparable<Superhero> { … @Override public int compareTo (Superhero this, Superhero that) { return this.strength.compareTo ( that.strength ); } } class Superhero implements Comparable<Superhero> { … public int compareTo (Superhero me, Superhero you) { return me.strength.compareTo ( you.strength ); } } class Superhero implements Comparator<Superhero> { … @Override public int compare(Superhero batman, Superhero superman) return batman.strength.compareTo ( superman.strength ); } } D. None ! A B C @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
import static java.util.stream.IntStream.range ; range(10, 0). forEach ( System.out :: println ); A. 0..10 B. IllegalArgumentException C. Nothing D. 10 .. 0 (10..0). each { println it } @ jbaruch
voxxeddayssingapore
javapuzzlersng
import static java.util.stream.IntStream.range ; range(10, 0). forEach ( System.out :: println ); A. 0..10 B. IllegalArgumentException C. Nothing D. 10 .. 0 @ jbaruch
voxxeddayssingapore
javapuzzlersng
10 – x) . forEach ( System.out :: println ) (10..0). each { println it } Srsly , how do you do it?! @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch
voxxeddayssingapore
javapuzzlersng
Conclusions @ jbaruch
voxxeddayssingapore
javapuzzlersng
@ jbaruch @ gamussa
Don’t abuse lambdas and streams !
@ jbaruch @ gamussa
jfrog.com / shownotes
@ jbaruch @ gamussa
/d e v/n u l l