TechTalk#2 - Java`s Final Keyword.key

publicité
Créateur d’applications mobiles
#TECHTALK
TechTalk gratuits et ouverts au public
au sein de nos locaux !
N’hésitez pas à vous inscrire aux prochaines
sessions sur notre site: ripplemotion-services.fr
#TECHTALK
TechTalk sous forme de courtes formations
le mercredi de 17h à 18h !
Échange entre vous et nos spécialistes en
développement mobile
RMS TECHTALK ANIMÉ PAR :
OLIVIER TABONE
Co-fondateur / Directeur
[email protected]
06 83 01 05 76
A few words about
JAVA’S FINAL KEYWORD
RMS TechTalk #2 - 19.10.2016
TechTalk #2 - 19.10.2016
OLIVIER TABONE
Co-fondateur / Directeur
A FEW WORDS ABOUT
Java’s Final Keyword
6
THE FINAL KEYWORD
TechTalk #2 - 19.10.2016
Provide a form of immutability to the Java language.
So what ?
OLIVIER TABONE
Co-fondateur / Directeur
Enforce immutability at compile time
So what ?
Programming errors are prevented by the compiler
A FEW WORDS ABOUT
Java’s Final Keyword
So what ?
Less crash in the application
So I write better code if I use the final keyword ?
Yes!
7
TechTalk #2 - 19.10.2016
THE FINAL KEYWORD
(The bad)
OLIVIER TABONE
Co-fondateur / Directeur
Applies to class, members, variables and
methods
Semantics varies !
A FEW WORDS ABOUT
Java’s Final Keyword
Benefits varies !
Performance varies !
8
TechTalk #2 - 19.10.2016
FINAL VARIABLES
use when required
The variable must be assigned once and only once.
OLIVIER TABONE
Co-fondateur / Directeur
A FEW WORDS ABOUT
Java’s Final Keyword
Immutable value semantics for raw types (long, int, …)
As java has no immutability for reference types, this is kind of
useless.
No performance benefits (on Dalvik + ART/AOT + ART/JIT)
‣ Use when required = Anonymous inner class definition
‣ Use when variable’s scope is large and you want to prevent a
reassignment by mistake
9
TechTalk #2 - 19.10.2016
FINAL MEMBERS
private final static
OLIVIER TABONE
Co-fondateur / Directeur
Use to define constants. Constants are
immutable, right ?
A FEW WORDS ABOUT
Java’s Final Keyword
10
TechTalk #2 - 19.10.2016
FINAL MEMBERS
private final
OLIVIER TABONE
Co-fondateur / Directeur
Provide atomicity for object creation. Object is wether
fully created or not.
A FEW WORDS ABOUT
Java’s Final Keyword
a class with only final immutable members is
immutable !
11
TechTalk #2 - 19.10.2016
OLIVIER TABONE
Co-fondateur / Directeur
FINAL MEMBERS
Use as much as possible *,
immutability is good design.
* can’t be used in Activity and Fragment subclasses :-(
A FEW WORDS ABOUT
Java’s Final Keyword
no performance benefits
12
TechTalk #2 - 19.10.2016
FINAL CLASS
Designed for inheritance or not
OLIVIER TABONE
Co-fondateur / Directeur
A FEW WORDS ABOUT
Java’s Final Keyword
Classes are designed to be inherited or not to be
inherited.
If you don’t know, your class is not designed to be
inherited.
using the “final class” modifier indicates your class is
not designed to be inherited.
final class should be the default
13
TechTalk #2 - 19.10.2016
FINAL CLASS
Do not design for inheritance
OLIVIER TABONE
Co-fondateur / Directeur
A FEW WORDS ABOUT
Java’s Final Keyword
Inheritance is hard
Inheritance is fragile
Inheritance takes time
Inheritance will make you sad
Design for Inheritance is the exception.
final class is the norm.
14
TechTalk #2 - 19.10.2016
FINAL CLASS
Provide some performance gains
OLIVIER TABONE
Co-fondateur / Directeur
A FEW WORDS ABOUT
Java’s Final Keyword
Performance benefits with ART/AOT (Android 6)
No performance benefits for other virtual machines :
Dalvik (Android < 6)
Hotspot (Desktop)
ART/JIT (Android 7)
15
TechTalk #2 - 19.10.2016
TAKE AWAYS
DO use final keyword…
OLIVIER TABONE
Co-fondateur / Directeur
…for classes to restrict inheritance
…for members so that objects are fully initialized
…for static member so that constants are
immutable
A FEW WORDS ABOUT
Java’s Final Keyword
DONT use final keyword…
…for variable (unless required)
…invoking a performance gain
16
Merci de votre participation !
OLIVIER TABONE
Co-fondateur / Directeur
[email protected]
06 83 01 05 76
Créateur d’applications mobiles
Téléchargement