SlideShare a Scribd company logo
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Developer Advocate @JFrog
@jbaruch on the internetz
Solution Architect @Confluent
@gAmUssA on the internetz
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
@tagir_valeev
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
1. Two entertaining guys on
the stage
2. Funny puzzling questions
3. You think and vote
4.T-shirts are airborne
5. Official twitter hashtag:
#javapuzzlersng
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Which Java version are you on?
A. Java 7
B. Java 8
C. Java 9
D. Java 6
E. Java 5
F. Java 2
@jbaruch http://jfrog.com/shownotes @gamussa
Watching the puzzlers like… #dafaq
@jbaruch http://jfrog.com/shownotes @gamussa
Everything works (or doesn't)
in the latest Java 8 and/or 9 update
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
A. IllegalArgumentException –can’t create semaphore with negative
B. UnsupportedOperationException –can’t drain when negative
C. 0
D. -42
@jbaruch http://jfrog.com/shownotes @gamussa
A. IllegalArgumentException –can’t create semaphore with negative
B. UnsupportedOperationException –can’t drain when negative
C. 0
D. -42
public class PerfectRobbery {
private Semaphore bankAccount = new Semaphore(-42);
public static void main(String[] args) {
PerfectRobbery perfectRobbery = new PerfectRobbery();
perfectRobbery.takeAllMoney();
perfectRobbery.checkBalance();
}
public void takeAllMoney(){
bankAccount.drainPermits();
}
public void checkBalance(){
System.out.println(bankAccount.availablePermits());
}
}
Available
-42?!
@jbaruch http://jfrog.com/shownotes @gamussa
Available
-42?!
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
A. true/true
B. true/false
C. false/true
D. false/false
Collections.emptyList() == Collections.emptyList();
Collections.emptyIterator() == Collections.emptyIterator();
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
Singleton Strikes Back!
@jbaruch http://jfrog.com/shownotes @gamussa
A. true/true
B. true/false
C. false/true
D. false/false
Spliterators.emptySpliterator() == Spliterators.emptySpliterator();
Stream.empty() == Stream.empty();
@jbaruch http://jfrog.com/shownotes @gamussa
Even empty Stream has state!
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
”Identical”
1. Has the same state
2. Not related to “equals and hashcode”contract
3. Not related to references to objects in memory
@jbaruch http://jfrog.com/shownotes @gamussa
List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
B. Absolutely identical non-empty lists
C. Non-identical empty lists
D. Non-identical non-empty lists
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
List[] twins = new List[2];
Arrays.setAll(twins, ArrayList::new);
A. Absolutely identical empty lists
B. Absolutely identical non-empty lists
C. Non-identical empty lists
D. Non-identical non-empty lists
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
How single is a Single Abstract Method Interface?
@jbaruch http://jfrog.com/shownotes @gamussa
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 http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
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 http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Hacking the bank
☑Bank software written in Java
☑Hack into it
☑Analyze the accounts
@jbaruch http://jfrog.com/shownotes @gamussa
Given the code above,which statement is wrong:
A. The Set is ordered by hashcode
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String>	accounts=	new	HashSet<>(Arrays.asList("Gates",	"Buffett",	"Bezos",	"Zuckerberg"));
System.out.println(”accounts=	"	+	accounts);
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Given the code above,which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String>	accounts=	new	HashSet<>(Arrays.asList("Gates",	"Buffett",	"Bezos",	"Zuckerberg"));
System.out.println(”accounts=	"	+	accounts);
@jbaruch http://jfrog.com/shownotes @gamussa
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
@jbaruch http://jfrog.com/shownotes @gamussa
Your turn,FBI
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
Given the code above,which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String>	accounts	=	Set.of("Gates",	"Buffett",	"Bezos",	"Zuckerberg");
System.out.println(”accounts=	"	+	accounts);
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
Given the code above,which statement is wrong:
A. The Set is ordered
B. The order is predictable across multiple runs of the JVM on the same machine
C. The order of elements in Set is not predictable
D. Statements A & B are correct
Set<String>	accounts	=	Set.of("Gates",	"Buffett",	"Bezos",	"Zuckerberg");
System.out.println(”accounts=	"	+	accounts);
@jbaruch http://jfrog.com/shownotes @gamussa
private int probe(Object pe) {
int idx = Math.floorMod(pe.hashCode() ^ SALT, elements.length);
while (true) {
E ee = elements[idx];
if (ee == null) {
return -idx - 1;
} else if (pe.equals(ee)) {
return idx;
} else if (++idx == elements.length) {
idx = 0;
}
}
}
@jbaruch http://jfrog.com/shownotes @gamussa
Juggling Accident
What’s correct?
A. If you convert your application to module,classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,you
have to declare it in module-info in order to use
C. Once you added the module-info to your project you have to
declare the dependencies twice,in classpath and in module-
info
D. None of the above
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
What’s correct?
A. If you convert your application to module,classpath
dependencies will still be resolved correctly
B. If one of the dependencies was converted to a module,you
have to declare it in module-info in order to use
C. Once you added the module-info to your project you have to
declare the dependencies twice,in classpath and in module-
info
D. None of the above
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
A. You killed them all
B. You killed only even ones
C. They all survived
D. You killed only odd ones
E. All answers are correct
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
if (heroes.hasNext()) {
heroes.next();
heroes.remove();
}
});
System.out.println(expendables);
}
@jbaruch http://jfrog.com/shownotes @gamussa
A. You killed them all
B. You killed only even ones
C. They all survived
D. You killed only odd ones
E. All answers are correct
static void killThemAll(Collection<Hero> expendables) {
Iterator<Hero> heroes = expendables.iterator();
heroes.forEachRemaining(e -> {
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 http://jfrog.com/shownotes @gamussa
Subtle Difference
@jbaruch http://jfrog.com/shownotes @gamussa
A. Both work just fine
B. Lambda works,method ref fails
C. Method ref works,lambda fails
D. Won’t compile
@FunctionalInterface
public interface OriginalPredicate<T> {
boolean test(T t);
}
OriginalPredicate<Object> lambda = (Object obj) -> ”adidas".equals(obj);
OriginalPredicate<Object> methodRef = ”adidas"::equals;
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
A. Both work just fine
B. Lambda works,method ref fails
C. Method ref works,lambda fails
D. Not a functional interface,will fail on annotation processing
@FunctionalInterface
Public interface CopyCatPredicate {
<T> boolean test(T t);
}
CopyCatPredicate lambda = (Object obj) -> " adadas".equals(obj);
CopyCatPredicate methodRef = " adadas"::equals;
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
A. Both work just fine
B. Lambda works,method ref fails
C. Method ref works,lambda fails
D. Not a functional interface,will fail on annotation processing
@FunctionalInterface
Public interface CopyCatPredicate {
<T> boolean test(T t);
}
CopyCatPredicate lambda = (Object obj) -> " adadas".equals(obj);
CopyCatPredicate methodRef = " adadas"::equals;
@jbaruch http://jfrog.com/shownotes @gamussa
A	generic	function	type	for	a	functional	interface	may	be	implemented	by	a	
method	reference	expression	(§15.13),	but	not	by	a	lambda	expression	
(§15.27)	as	there	is	no	syntax	for	generic	lambda	expressions.	“
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
A.[Data, Kirk, Spock]
B.[Spock, Kirk, Data, Data, Kirk, Spock]
C.[Spock, Kirk, Data]
D.[Data, Data, Kirk, Kirk, Spock, Spock]
E.Are you nuts? Won’t compile! Data with
Kirk?!
List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk",
"Spock").sequential()
.filter(new TreeSet<>()::add).collect(Collectors.toList());
System.out.println(list);
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
A.[Data, Kirk, Spock]
B.[Spock, Kirk, Data, Data, Kirk, Spock]
C.[Spock, Kirk, Data]
D.[Data, Data, Kirk, Kirk, Spock, Spock]
E.Are you nuts? Won’t compile! Data with
Kirk?!
List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk",
"Spock").sequential()
.filter(new TreeSet<>()::add).collect(Collectors.toList());
System.out.println(list);
@jbaruch http://jfrog.com/shownotes @gamussa
Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group
A.[Data, Kirk, Spock]
B.[Spock, Kirk, Data, Data, Kirk, Spock]
C.[Spock, Kirk, Data]
D.[Data, Data, Kirk, Kirk, Spock, Spock]
E.Are you nuts? Won’t compile! Data with
Kirk?!
List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk",
"Spock").sequential()
.filter(new TreeSet<>()::add).collect(Collectors.toList());
System.out.println(list);
@jbaruch http://jfrog.com/shownotes @gamussa
filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!=
New	instance	is	
created	every	time!
Instance	method	is	
created	once!
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
Will	never	happen
Will	never	happen
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
A. obvious / obvious
B. obvious / NullPointerException
C. NullPointerException / obvious
D. NullPointerException / NullPointerException
Optional.of("obvious").orElseGet(null);
Optional.empty().map(null).orElse("obvious");
@jbaruch http://jfrog.com/shownotes @gamussa
@jbaruch http://jfrog.com/shownotes @gamussa
Identical?
@jbaruch http://jfrog.com/shownotes @gamussa
A. All
B. 3 and 4
C. Only 3
D. Other
1. Consumer<String> agentA = s -> System.out.println(s);
Consumer<String> agentB = s -> System.out.println(s);
2. Consumer<String> agentA = System.out::println;
Consumer<String> agentB = System.out::println;
3. Supplier<Consumer<String>> supplier = () -> s -> System.out.println(s);
Consumer<String> agentA = supplier.get();
Consumer<String> agentB = supplier.get();
4. Supplier<Consumer<String>> supplier = () -> System.out::println;
Consumer<String> agentA = supplier.get();
Consumer<String> agentB = supplier.get();
When agentA == agentB?
@jbaruch http://jfrog.com/shownotes @gamussa
A. All
B. 3 and 4
C. Only 3
D. Other
1. Consumer<String> agentA = s -> System.out.println(s);
Consumer<String> agentB = s -> System.out.println(s);
2. Consumer<String> agentA = System.out::println;
Consumer<String> agentB = System.out::println;
3. Supplier<Consumer<String>> supplier = () -> s -> System.out.println(s);
Consumer<String> agentA = supplier.get();
Consumer<String> agentB = supplier.get();
4. Supplier<Consumer<String>> supplier = () -> System.out::println;
Consumer<String> agentA = supplier.get();
Consumer<String> agentB = supplier.get();
When agentA == agentB?
Reuse is only possible for pure functions
Consumers accept parameters == have
state
Supplier in 4 has state –the resolved
method reference
@jbaruch http://jfrog.com/shownotes @gamussa
Conclusions
@jbaruch http://jfrog.com/shownotes @gamussa
-Write readable code!
-Comment all the tricks
-Sometimes it’s just a bug
-Static code analysis FTW -
IntelliJ IDEA!
-RTFM!
-Don’t abuse lambdas and
streams!
-Trust us, we have much
more where those came
from.
-Puzzlers? Gotchas? Fetal
position inducing behavior?
-puzzlers@jfrog.com
-Shownotes!
-http://jfrog.com/shownotes
-Slides
-Video in 24 hours
-Links
-Ratings
-Raffle! (come early)
-Did you like it?
-Praise us on twitter!
-#javapuzzlersng
-@gamussa
-@jbaruch
-Didn’t like it?
-/dev/null

More Related Content

Similar to Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group (20)

TXT
CORE JAVA
Shohan Ahmed
 
PDF
1z0 851 exam-java standard edition 6 programmer certified professional
Isabella789
 
DOCX
Java Questions
bindur87
 
PPTX
Java SE 17 Study Guide for Certification - Chapter 01
williamrobertherman
 
PPTX
131 Lab slides (all in one)
Tak Lee
 
PDF
Java MCQ Important Questions and Answers
SONU HEETSON
 
PDF
[Philly ETE] Java Puzzlers NG
Viktor Gamov
 
DOCX
Simulado java se 7 programmer
Miguel Vilaca
 
DOC
Java questions1
yash4884
 
DOC
MX Server is my friend
Gabriel Daty
 
DOC
MX Server is my friend
Gabriel Daty
 
DOC
MX Server is my friend
Gabriel Daty
 
DOC
MX Server is my friend
Gabriel Daty
 
PPTX
Master the Concepts Behind the Java 10 Challenges and Eliminate Stressful Bugs
Rafael Chinelato Del Nero
 
PPTX
More topics on Java
Ahmed Misbah
 
PPTX
AP Computer Science AP Review 2011 FRQ Questions
lipakhon2026
 
PPTX
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
PPTX
Language fundamentals ocjp
Bhavishya sharma
 
TXT
Indus Valley Partner aptitude questions and answers
Sushant Choudhary
 
CORE JAVA
Shohan Ahmed
 
1z0 851 exam-java standard edition 6 programmer certified professional
Isabella789
 
Java Questions
bindur87
 
Java SE 17 Study Guide for Certification - Chapter 01
williamrobertherman
 
131 Lab slides (all in one)
Tak Lee
 
Java MCQ Important Questions and Answers
SONU HEETSON
 
[Philly ETE] Java Puzzlers NG
Viktor Gamov
 
Simulado java se 7 programmer
Miguel Vilaca
 
Java questions1
yash4884
 
MX Server is my friend
Gabriel Daty
 
MX Server is my friend
Gabriel Daty
 
MX Server is my friend
Gabriel Daty
 
MX Server is my friend
Gabriel Daty
 
Master the Concepts Behind the Java 10 Challenges and Eliminate Stressful Bugs
Rafael Chinelato Del Nero
 
More topics on Java
Ahmed Misbah
 
AP Computer Science AP Review 2011 FRQ Questions
lipakhon2026
 
Proposals for new function in Java SE 9 and beyond
Barry Feigenbaum
 
Language fundamentals ocjp
Bhavishya sharma
 
Indus Valley Partner aptitude questions and answers
Sushant Choudhary
 

More from Baruch Sadogursky (20)

PDF
DevOps Patterns & Antipatterns for Continuous Software Updates @ NADOG April ...
Baruch Sadogursky
 
PDF
DevOps Patterns & Antipatterns for Continuous Software Updates @ DevOps.com A...
Baruch Sadogursky
 
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code NY...
Baruch Sadogursky
 
PDF
Data driven devops as presented at QCon London 2018
Baruch Sadogursky
 
PDF
A Research Study Into DevOps Bottlenecks as presented at Oracle Code LA 2018
Baruch Sadogursky
 
PDF
Java Puzzlers NG S03 a DevNexus 2018
Baruch Sadogursky
 
PDF
Where the Helm are your binaries? as presented at Canada Kubernetes Meetups
Baruch Sadogursky
 
PDF
Data driven devops as presented at Codemash 2018
Baruch Sadogursky
 
PDF
A Research Study into DevOps Bottlenecks as presented at Codemash 2018
Baruch Sadogursky
 
PPTX
Best Practices for Managing Docker Versions as presented at JavaOne 2017
Baruch Sadogursky
 
PDF
Troubleshooting & Debugging Production Microservices in Kubernetes as present...
Baruch Sadogursky
 
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Devoxx 2017
Baruch Sadogursky
 
PPTX
Amazon Alexa Skills vs Google Home Actions, the Big Java VUI Faceoff as prese...
Baruch Sadogursky
 
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at DevOps Days Be...
Baruch Sadogursky
 
PDF
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at The Pittsburgh...
Baruch Sadogursky
 
PDF
Let’s Wing It: A Study in DevRel Strategy
Baruch Sadogursky
 
PDF
Log Driven First Class Customer Support at Scale
Baruch Sadogursky
 
PPTX
[Webinar] The Frog And The Butler: CI Pipelines For Modern DevOps
Baruch Sadogursky
 
PDF
Patterns and antipatterns in Docker image lifecycle as was presented at DC Do...
Baruch Sadogursky
 
PDF
Groovy Puzzlers S04: The Bytecode Bites Back at Gr8Conf US 2017
Baruch Sadogursky
 
DevOps Patterns & Antipatterns for Continuous Software Updates @ NADOG April ...
Baruch Sadogursky
 
DevOps Patterns & Antipatterns for Continuous Software Updates @ DevOps.com A...
Baruch Sadogursky
 
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Oracle Code NY...
Baruch Sadogursky
 
Data driven devops as presented at QCon London 2018
Baruch Sadogursky
 
A Research Study Into DevOps Bottlenecks as presented at Oracle Code LA 2018
Baruch Sadogursky
 
Java Puzzlers NG S03 a DevNexus 2018
Baruch Sadogursky
 
Where the Helm are your binaries? as presented at Canada Kubernetes Meetups
Baruch Sadogursky
 
Data driven devops as presented at Codemash 2018
Baruch Sadogursky
 
A Research Study into DevOps Bottlenecks as presented at Codemash 2018
Baruch Sadogursky
 
Best Practices for Managing Docker Versions as presented at JavaOne 2017
Baruch Sadogursky
 
Troubleshooting & Debugging Production Microservices in Kubernetes as present...
Baruch Sadogursky
 
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at Devoxx 2017
Baruch Sadogursky
 
Amazon Alexa Skills vs Google Home Actions, the Big Java VUI Faceoff as prese...
Baruch Sadogursky
 
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at DevOps Days Be...
Baruch Sadogursky
 
DevOps @Scale (Greek Tragedy in 3 Acts) as it was presented at The Pittsburgh...
Baruch Sadogursky
 
Let’s Wing It: A Study in DevRel Strategy
Baruch Sadogursky
 
Log Driven First Class Customer Support at Scale
Baruch Sadogursky
 
[Webinar] The Frog And The Butler: CI Pipelines For Modern DevOps
Baruch Sadogursky
 
Patterns and antipatterns in Docker image lifecycle as was presented at DC Do...
Baruch Sadogursky
 
Groovy Puzzlers S04: The Bytecode Bites Back at Gr8Conf US 2017
Baruch Sadogursky
 
Ad

Recently uploaded (20)

PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Ad

Java Puzzlers NG S02: Down the Rabbit Hole as it was presented at The Pittsburgh Java Meetup Group

  • 3. Developer Advocate @JFrog @jbaruch on the internetz Solution Architect @Confluent @gAmUssA on the internetz
  • 9. 1. Two entertaining guys on the stage 2. Funny puzzling questions 3. You think and vote 4.T-shirts are airborne 5. Official twitter hashtag: #javapuzzlersng
  • 11. Which Java version are you on? A. Java 7 B. Java 8 C. Java 9 D. Java 6 E. Java 5 F. Java 2 @jbaruch http://jfrog.com/shownotes @gamussa
  • 12. Watching the puzzlers like… #dafaq @jbaruch http://jfrog.com/shownotes @gamussa
  • 13. Everything works (or doesn't) in the latest Java 8 and/or 9 update @jbaruch http://jfrog.com/shownotes @gamussa
  • 16. public class PerfectRobbery { private Semaphore bankAccount = new Semaphore(-42); public static void main(String[] args) { PerfectRobbery perfectRobbery = new PerfectRobbery(); perfectRobbery.takeAllMoney(); perfectRobbery.checkBalance(); } public void takeAllMoney(){ bankAccount.drainPermits(); } public void checkBalance(){ System.out.println(bankAccount.availablePermits()); } } A. IllegalArgumentException –can’t create semaphore with negative B. UnsupportedOperationException –can’t drain when negative C. 0 D. -42
  • 18. A. IllegalArgumentException –can’t create semaphore with negative B. UnsupportedOperationException –can’t drain when negative C. 0 D. -42 public class PerfectRobbery { private Semaphore bankAccount = new Semaphore(-42); public static void main(String[] args) { PerfectRobbery perfectRobbery = new PerfectRobbery(); perfectRobbery.takeAllMoney(); perfectRobbery.checkBalance(); } public void takeAllMoney(){ bankAccount.drainPermits(); } public void checkBalance(){ System.out.println(bankAccount.availablePermits()); } }
  • 22. A. true/true B. true/false C. false/true D. false/false Collections.emptyList() == Collections.emptyList(); Collections.emptyIterator() == Collections.emptyIterator(); @jbaruch http://jfrog.com/shownotes @gamussa
  • 24. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator(); Stream.empty() == Stream.empty();
  • 25. Singleton Strikes Back! @jbaruch http://jfrog.com/shownotes @gamussa
  • 26. A. true/true B. true/false C. false/true D. false/false Spliterators.emptySpliterator() == Spliterators.emptySpliterator(); Stream.empty() == Stream.empty(); @jbaruch http://jfrog.com/shownotes @gamussa
  • 27. Even empty Stream has state! @jbaruch http://jfrog.com/shownotes @gamussa
  • 29. ”Identical” 1. Has the same state 2. Not related to “equals and hashcode”contract 3. Not related to references to objects in memory @jbaruch http://jfrog.com/shownotes @gamussa
  • 30. List[] twins = new List[2]; Arrays.setAll(twins, ArrayList::new); A. Absolutely identical empty lists B. Absolutely identical non-empty lists C. Non-identical empty lists D. Non-identical non-empty lists @jbaruch http://jfrog.com/shownotes @gamussa
  • 32. List[] twins = new List[2]; Arrays.setAll(twins, ArrayList::new); A. Absolutely identical empty lists B. Absolutely identical non-empty lists C. Non-identical empty lists D. Non-identical non-empty lists @jbaruch http://jfrog.com/shownotes @gamussa
  • 34. How single is a Single Abstract Method Interface? @jbaruch http://jfrog.com/shownotes @gamussa
  • 35. 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 http://jfrog.com/shownotes @gamussa
  • 37. 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 http://jfrog.com/shownotes @gamussa
  • 40. Hacking the bank ☑Bank software written in Java ☑Hack into it ☑Analyze the accounts @jbaruch http://jfrog.com/shownotes @gamussa
  • 41. Given the code above,which statement is wrong: A. The Set is ordered by hashcode B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg")); System.out.println(”accounts= " + accounts); @jbaruch http://jfrog.com/shownotes @gamussa
  • 43. Given the code above,which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts= new HashSet<>(Arrays.asList("Gates", "Buffett", "Bezos", "Zuckerberg")); System.out.println(”accounts= " + accounts); @jbaruch http://jfrog.com/shownotes @gamussa
  • 44. public boolean add(E e) { return map.put(e, PRESENT)==null; } @jbaruch http://jfrog.com/shownotes @gamussa
  • 47. Given the code above,which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg"); System.out.println(”accounts= " + accounts); @jbaruch http://jfrog.com/shownotes @gamussa
  • 50. Given the code above,which statement is wrong: A. The Set is ordered B. The order is predictable across multiple runs of the JVM on the same machine C. The order of elements in Set is not predictable D. Statements A & B are correct Set<String> accounts = Set.of("Gates", "Buffett", "Bezos", "Zuckerberg"); System.out.println(”accounts= " + accounts); @jbaruch http://jfrog.com/shownotes @gamussa
  • 51. private int probe(Object pe) { int idx = Math.floorMod(pe.hashCode() ^ SALT, elements.length); while (true) { E ee = elements[idx]; if (ee == null) { return -idx - 1; } else if (pe.equals(ee)) { return idx; } else if (++idx == elements.length) { idx = 0; } } } @jbaruch http://jfrog.com/shownotes @gamussa
  • 53. What’s correct? A. If you convert your application to module,classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module,you have to declare it in module-info in order to use C. Once you added the module-info to your project you have to declare the dependencies twice,in classpath and in module- info D. None of the above @jbaruch http://jfrog.com/shownotes @gamussa
  • 55. What’s correct? A. If you convert your application to module,classpath dependencies will still be resolved correctly B. If one of the dependencies was converted to a module,you have to declare it in module-info in order to use C. Once you added the module-info to your project you have to declare the dependencies twice,in classpath and in module- info D. None of the above @jbaruch http://jfrog.com/shownotes @gamussa
  • 58. A. You killed them all B. You killed only even ones C. They all survived D. You killed only odd ones E. All answers are correct static void killThemAll(Collection<Hero> expendables) { Iterator<Hero> heroes = expendables.iterator(); heroes.forEachRemaining(e -> { if (heroes.hasNext()) { heroes.next(); heroes.remove(); } }); System.out.println(expendables); }
  • 60. A. You killed them all B. You killed only even ones C. They all survived D. You killed only odd ones E. All answers are correct static void killThemAll(Collection<Hero> expendables) { Iterator<Hero> heroes = expendables.iterator(); heroes.forEachRemaining(e -> { if (heroes.hasNext()) { heroes.next(); heroes.remove(); } }); System.out.println(expendables); }
  • 61. 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 http://jfrog.com/shownotes @gamussa
  • 63. A. Both work just fine B. Lambda works,method ref fails C. Method ref works,lambda fails D. Won’t compile @FunctionalInterface public interface OriginalPredicate<T> { boolean test(T t); } OriginalPredicate<Object> lambda = (Object obj) -> ”adidas".equals(obj); OriginalPredicate<Object> methodRef = ”adidas"::equals;
  • 65. A. Both work just fine B. Lambda works,method ref fails C. Method ref works,lambda fails D. Not a functional interface,will fail on annotation processing @FunctionalInterface Public interface CopyCatPredicate { <T> boolean test(T t); } CopyCatPredicate lambda = (Object obj) -> " adadas".equals(obj); CopyCatPredicate methodRef = " adadas"::equals;
  • 67. A. Both work just fine B. Lambda works,method ref fails C. Method ref works,lambda fails D. Not a functional interface,will fail on annotation processing @FunctionalInterface Public interface CopyCatPredicate { <T> boolean test(T t); } CopyCatPredicate lambda = (Object obj) -> " adadas".equals(obj); CopyCatPredicate methodRef = " adadas"::equals;
  • 71. A.[Data, Kirk, Spock] B.[Spock, Kirk, Data, Data, Kirk, Spock] C.[Spock, Kirk, Data] D.[Data, Data, Kirk, Kirk, Spock, Spock] E.Are you nuts? Won’t compile! Data with Kirk?! List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential() .filter(new TreeSet<>()::add).collect(Collectors.toList()); System.out.println(list); @jbaruch http://jfrog.com/shownotes @gamussa
  • 73. A.[Data, Kirk, Spock] B.[Spock, Kirk, Data, Data, Kirk, Spock] C.[Spock, Kirk, Data] D.[Data, Data, Kirk, Kirk, Spock, Spock] E.Are you nuts? Won’t compile! Data with Kirk?! List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential() .filter(new TreeSet<>()::add).collect(Collectors.toList()); System.out.println(list); @jbaruch http://jfrog.com/shownotes @gamussa
  • 75. A.[Data, Kirk, Spock] B.[Spock, Kirk, Data, Data, Kirk, Spock] C.[Spock, Kirk, Data] D.[Data, Data, Kirk, Kirk, Spock, Spock] E.Are you nuts? Won’t compile! Data with Kirk?! List<String> list = Stream.of("Spock", "Kirk", "Data", "Data", "Kirk", "Spock").sequential() .filter(new TreeSet<>()::add).collect(Collectors.toList()); System.out.println(list); @jbaruch http://jfrog.com/shownotes @gamussa
  • 76. filter(new TreeSet<>()::add) filter(i -> new TreeSet<>().add(i))!= New instance is created every time! Instance method is created once! @jbaruch http://jfrog.com/shownotes @gamussa
  • 78. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null); Optional.empty().map(null).orElse("obvious"); @jbaruch http://jfrog.com/shownotes @gamussa
  • 80. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null); Optional.empty().map(null).orElse("obvious"); Will never happen Will never happen @jbaruch http://jfrog.com/shownotes @gamussa
  • 82. A. obvious / obvious B. obvious / NullPointerException C. NullPointerException / obvious D. NullPointerException / NullPointerException Optional.of("obvious").orElseGet(null); Optional.empty().map(null).orElse("obvious"); @jbaruch http://jfrog.com/shownotes @gamussa
  • 85. A. All B. 3 and 4 C. Only 3 D. Other 1. Consumer<String> agentA = s -> System.out.println(s); Consumer<String> agentB = s -> System.out.println(s); 2. Consumer<String> agentA = System.out::println; Consumer<String> agentB = System.out::println; 3. Supplier<Consumer<String>> supplier = () -> s -> System.out.println(s); Consumer<String> agentA = supplier.get(); Consumer<String> agentB = supplier.get(); 4. Supplier<Consumer<String>> supplier = () -> System.out::println; Consumer<String> agentA = supplier.get(); Consumer<String> agentB = supplier.get(); When agentA == agentB?
  • 87. A. All B. 3 and 4 C. Only 3 D. Other 1. Consumer<String> agentA = s -> System.out.println(s); Consumer<String> agentB = s -> System.out.println(s); 2. Consumer<String> agentA = System.out::println; Consumer<String> agentB = System.out::println; 3. Supplier<Consumer<String>> supplier = () -> s -> System.out.println(s); Consumer<String> agentA = supplier.get(); Consumer<String> agentB = supplier.get(); 4. Supplier<Consumer<String>> supplier = () -> System.out::println; Consumer<String> agentA = supplier.get(); Consumer<String> agentB = supplier.get(); When agentA == agentB?
  • 88. Reuse is only possible for pure functions Consumers accept parameters == have state Supplier in 4 has state –the resolved method reference @jbaruch http://jfrog.com/shownotes @gamussa
  • 90. -Write readable code! -Comment all the tricks -Sometimes it’s just a bug -Static code analysis FTW - IntelliJ IDEA! -RTFM! -Don’t abuse lambdas and streams!
  • 91. -Trust us, we have much more where those came from. -Puzzlers? Gotchas? Fetal position inducing behavior? [email protected]
  • 93. -Did you like it? -Praise us on twitter! -#javapuzzlersng -@gamussa -@jbaruch -Didn’t like it? -/dev/null