Save 37% off PRO during our Black Friday Sale! »

Java Value Types

Java Value Types

Slides from my talk "Java Value types – The Next Big Thing for Java" most recently held at JavaZone 2017

575ca492bac55e895d0e1c86f7d709fe?s=128

Henning Schwentner

September 14, 2017
Tweet

Transcript

  1. Starring With vs Special Appearances by

  2. None
  3. @hschwentner Value types – The Next Big Thing for Java

  4. None
  5. #javazone

  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. HENNING

  14. @hschwentner SCHWENTNER

  15. @hschwentner SHVANTNA

  16. None
  17. @hschwentner

  18. @hshvantna

  19. None
  20. None
  21. None
  22. None
  23. @hschwentner speakerdeck.com/hschwentner

  24. Quelle: http://www.schulbilder.org/malvorlage-informatiker-i10418.html

  25. None
  26. None
  27. Fritz & Franz & Lothar & Philipp.

  28. None
  29. None
  30. None
  31. None
  32. None
  33. Foto: Kjetil Ree/Wikipedia

  34. Foto: Steindy/Wikipedia

  35. None
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. Whisky Papa Sierra

  46. None
  47. ”Enjoyable Business-Software”

  48. None
  49. ”Enjoyable Business-Software”

  50. Value Types for Java

  51. @hschwentner DISCLAIMER

  52. None
  53. None
  54. None
  55. None
  56. None
  57. Value Types for Java

  58. @hschwentner ?

  59. None
  60. None
  61. None
  62. @BrianGoetz

  63. None
  64. None
  65. None
  66. @hschwentner λ

  67. None
  68. java.util.Optional<T>

  69. None
  70. None
  71. None
  72. None
  73. None
  74. None
  75. @hschwentner “The next big thing for java are value types!”

  76. None
  77. @hschwentner ?

  78. None
  79. None
  80. None
  81. None
  82. None
  83. None
  84. None
  85. None
  86. None
  87. Identity

  88. None
  89. None
  90. None
  91. None
  92. None
  93. @hschwentner Object: *Identity *Lifecycle *(mutable state)

  94. None
  95. None
  96. None
  97. None
  98. @hschwentner

  99. @hschwentner Value: *No identity *immutable

  100. @hschwentner Example

  101. None
  102. None
  103. None
  104. None
  105. None
  106. None
  107. None
  108. None
  109. None
  110. None
  111. None
  112. None
  113. None
  114. None
  115. None
  116. None
  117. None
  118. None
  119. None
  120. None
  121. None
  122. None
  123. 3

  124. 3 3

  125. 3

  126. @hschwentner Where does an object live actually?

  127. None
  128. None
  129. None
  130. None
  131. None
  132. None
  133. None
  134. compile time

  135. None
  136. run time

  137. @hschwentner Where does an object live actually?

  138. None
  139. 3

  140. 3 3

  141. None
  142. int boolean double float

  143. unsigned complex

  144. iban postcode amount

  145. None
  146. value-based class

  147. @hschwentner Only final fields

  148. @hschwentner No state-changing methods

  149. @hschwentner equals() does not depend on identity

  150. value-based class

  151. But

  152. 3 3

  153. @hschwentner equals() ==

  154. None
  155. 3

  156. by value

  157. by reference

  158. 3

  159. None
  160. Memory*2

  161. 3

  162. 3

  163. None
  164. None
  165. int[]

  166. 3 5 10 5 0

  167. Object[]

  168. 3 5 10 5 0

  169. 3 10 5 0

  170. @hschwentner The answer

  171. None
  172. None
  173. @hschwentner “Works like an int – codes like a class”

  174. @hschwentner Example

  175. y x

  176. y x

  177. @hschwentner (x, y)

  178. None
  179. None
  180. None
  181. None
  182. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  183. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  184. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  185. None
  186. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  187. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  188. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  189. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  190. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  191. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  192. final __ByValue class Point { private final int _x; private

    final int _y; public Point(int x, int y) { _x = x; _y = y; } public boolean equals(Point other) { return _x == other._x && _y == other._y; } }
  193. @hschwentner “Works like an int – codes like a class”

  194. @hschwentner “Works like an int – codes like a class”

  195. Point p = __MakeValue(4, 7);

  196. Point p = __MakeValue(4, 7);

  197. Point p = new Point(4, 7);

  198. Point p = Point(4, 7);

  199. Point p = (4, 7);

  200. 4 7

  201. 4 7

  202. None
  203. None
  204. 4 7

  205. 4 7

  206. 4 7

  207. None
  208. None
  209. Point p = __MakeValue Point(4, 7);

  210. @hschwentner ==

  211. @hschwentner !=

  212. Default value

  213. None
  214. None
  215. @hschwentner What else is there?

  216. List<Point>

  217. List<int>

  218. None
  219. List<Point>

  220. None
  221. None
  222. None
  223. Software != End

  224. None
  225. ?

  226. None
  227. None
  228. None
  229. None
  230. None
  231. Value Object

  232. @hschwentner Domain values

  233. Postal code GPS coordinate IBAN Container number IATA code Amount

  234. Ubiquitous Language

  235. Allgegenwärtige Sprache

  236. Fach- sprache

  237. None
  238. Ludwig Wittgenstein “Whereof one cannot speak, thereof one must be

    silent”
  239. Ludwig Wittgenstein »Wovon man nicht sprechen kann, darüber muss man

    schweigen.«
  240. @hschwentner Example

  241. public class BankAccount { private int _balance; public void withdraw(int

    amount) { _balance -= amount; } // ... }
  242. public class BankAccount { private int _balance; public void withdraw(int

    amount) { _balance -= amount; } // ... }
  243. public class BankAccount { private int _balance; public void withdraw(int

    amount) { _balance -= amount; } // ... }
  244. public class BankAccount { private int _balance; public void withdraw(int

    amount) { _balance -= amount; } // ... }
  245. public class BankAccount { private int _balance; public void withdraw(int

    amount) { _balance -= amount; } // ... }
  246. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } }
  247. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } }
  248. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } }
  249. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } }
  250. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } public void deposit(int amount, String currency) { assert currency.equals(_currency); _balance += amount; } }
  251. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } public void deposit(int amount, String currency) { assert currency.equals(_currency); _balance += amount; } }
  252. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } public void deposit(int amount, String currency) { assert currency.equals(_currency); _balance += amount; } }
  253. public class BankAccount { private int _balance; private String _currency;

    public void withdraw(int amount, String currency) { assert currency.equals(_currency); _balance -= amount; } public void deposit(int amount, String currency) { assert currency.equals(_currency); _balance += amount; } }
  254. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  255. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  256. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  257. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  258. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  259. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  260. public class Amount { private final int _amount; private final

    Currency _currency; public Amount(int amount, Currency currency) { _amount = amount; _currency = currency; } public Amount add(Amount other) { assert hasSameCurrency(other); return new Amount(_amount + other._amount, _currency); } public boolean hasSameCurrency(Amount other) { return other._currency == _currency; } }
  261. public class BankAccount { private Amount _balance; public void withdraw(Amount

    amount) { _balance.subtract(amount); } public void deposit(Amount amount) { _balance.add(amount); } }
  262. public class BankAccount { private Amount _balance; public void withdraw(Amount

    amount) { _balance.subtract(amount); } public void deposit(Amount amount) { _balance.add(amount); } }
  263. public class BankAccount { private Amount _balance; public void withdraw(Amount

    amount) { _balance.subtract(amount); } public void deposit(Amount amount) { _balance.add(amount); } }
  264. public class BankAccount { private Amount _balance; public void withdraw(Amount

    amount) { assert amount.isGreaterThan(_amount); _balance.subtract(amount); } public void deposit(Amount amount) { _balance.add(amount); } }
  265. Memory*2

  266. None
  267. None
  268. None
  269. public class Amount { // ... }

  270. public __ByValue class Amount { // ... }

  271. None
  272. public __ByValue class Amount { // ... }

  273. @hschwentner eat the cake and have it too

  274. @hschwentner real domain models and good performance

  275. @hschwentner And in other programming languages?

  276. C++

  277. None
  278. Point p;

  279. None
  280. Point p = Point(4, 7);

  281. 4 7

  282. Point * p_pointer;

  283. None
  284. Point * p_pointer = new Point(4, 7);

  285. 4 7

  286. Point & p_reference = new Point(4, 7);

  287. C++

  288. None
  289. struct

  290. 4 7

  291. 4 7

  292. None
  293. None
  294. None
  295. @hschwentner But

  296. @hschwentner Mutable state

  297. None
  298. Interesting concept: Nullable<T>

  299. None
  300. Scala

  301. Case Class

  302. @hschwentner Pattern matching

  303. @hschwentner Generated equals() based on the members

  304. @hschwentner Immutable

  305. value-based class

  306. None
  307. Data class

  308. @hschwentner In the end

  309. @hschwentner Key take aways: *value/object are different *domain model and

    performance *only strawman syntax yet
  310. None
  311. @hschwentner Further Reading

  312. project valhalla

  313. None
  314. None
  315. None
  316. None
  317. Workshop Domain-Driven Design concrete wps.de/ddd

  318. • F o t o : H e n n

    i n g S c h w e
  319. None
  320. None
  321. None
  322. @hschwentner FEEDBACK

  323. None
  324. None
  325. None
  326. None
  327. Henning Schwentner hs@wps.de @hschwentner a