Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Beauty & the Beast - Java VS TypeScript

119 views

Published on

Once upon a time, there was a poor, innocent language. It was friendly and kind. Everything could have been nice and peaceful, but there was another language that was grumpy and mean. One day they met and started to argue about who the better language was…. This session compares the Java language with TypeScript. It discusses how common problems are solved with these languages. At the end, you’ll see who is the beauty and who is the beast.

Published in: Technology
  • Be the first to comment

Beauty & the Beast - Java VS TypeScript

  1. 1. Karakun DevHub_ dev.karakun.com @net0pyr / @HendrikEbbers
  2. 2. Once upon a time in Coderland… Once upon a time in Coderland… @net0pyr / @HendrikEbbers
  3. 3. … lived a young coder that found this cool new language TypeScript
  4. 4. But one day the coder got lost in the woods of Coderland and found a magical old castle…
  5. 5. And after a while the coder and the beast get to know each other… In the castle lived a beast the coder had never seen before
  6. 6. And after a while the coder and the beast got to know each other…
  7. 7. Beauty and the Beast @net0pyr / @HendrikEbbers
  8. 8. Karakun DevHub_ dev.karakun.com About me • Karakun Co-Founder • Lead of JUG Dortmund • JSR EG member • JavaOne Rockstar, Java Champion • AdoptOpenJDK TSC @net0pyr / @HendrikEbbers
  9. 9. Karakun DevHub_ dev.karakun.com About me • Karakun Co-Founder • Lead of JUG Freiburg • Used to be: speaker, author,
 developer, … • Switched to the dark side @net0pyr / @HendrikEbbers
  10. 10. Karakun DevHub_ dev.karakun.com Java • "Oak" (Object Application Kernel) / "The Green Project" was developed in 1992 at Sun Microsystems • This project evolved to Java in 1995 @net0pyr / @HendrikEbbers
  11. 11. Karakun DevHub_ dev.karakun.com Java • In 1998 the JCP (Java Community 
 Project) was formed • Java is released under GNU GPL with classpath exception • Java is 100% open source (OpenJDK) and several vendors provide JDKs @net0pyr / @HendrikEbbers
  12. 12. Karakun DevHub_ dev.karakun.com TypeScript • First public appearance in 2012 • Developed by Microsoft • Open-source • Strict superset of JavaScript, adds optional static typing • Transcompiles to JavaScript @net0pyr / @HendrikEbbers
  13. 13. primitive datatypes @net0pyr / @HendrikEbbers
  14. 14. Karakun DevHub_ dev.karakun.com Variables let isDone: boolean = false; boolean isDone = false; @net0pyr / @HendrikEbbers
  15. 15. Karakun DevHub_ dev.karakun.com Variables let isDone: boolean = false; boolean isDone = false; Type Name Value @net0pyr / @HendrikEbbers
  16. 16. Karakun DevHub_ dev.karakun.com Variables let isDone: boolean = false; boolean isDone = false; ValueTypeName @net0pyr / @HendrikEbbers
  17. 17. Karakun DevHub_ dev.karakun.com Primitive Datatypes boolean v = false; int v = 1; long v = 1L; double v = 1.0d; float v = 1.0f; short v = 1; byte v = 1; char v = 'a'; let v: number = 6; let v: boolean = true; let v: string = "Hi"; @net0pyr / @HendrikEbbers
  18. 18. Karakun DevHub_ dev.karakun.com Primitive Datatypes • JavaScript numbers are always 64-bit floating point • Same behaviour in TypeScript var x = 999999999999999;   // x will be 999999999999999 var y = 9999999999999999;  // y will be 10000000000000000 var x = 0.2 + 0.1;         // x will be 0.30000000000000004 @net0pyr / @HendrikEbbers
  19. 19. Karakun DevHub_ dev.karakun.com Primitive Datatypes • String is not a primitive datatype in Java (see java.lang.String) • Java allows to create a String like primitive data • Internally the String class holds a char[] @net0pyr / @HendrikEbbers
  20. 20. Karakun DevHub_ dev.karakun.com Arrays and tuples • TypeScript provides native support for arrays and tuples • Java provides native support for arrays let vArray: number[] = [1, 2, 3]; let vTuple: [string, number]; int[] vArray = {1, 2, 3}; @net0pyr / @HendrikEbbers
  21. 21. Karakun DevHub_ dev.karakun.com Arrays and tuples • Instead of providing native tuples in the Java language syntax a more extensive feature is planed for future Java versions • With Records (JEP 169) you can easily create constructs like tuples (and much more) @net0pyr / @HendrikEbbers
  22. 22. methods and functions @net0pyr / @HendrikEbbers
  23. 23. Karakun DevHub_ dev.karakun.com Methods / Functions function isBig(x: number): boolean { return x > 10; } boolean isBig(int x) { return x > 10; } @net0pyr / @HendrikEbbers
  24. 24. Karakun DevHub_ dev.karakun.com Methods / Functions function isBig(x: number): boolean { return x > 10; } Param Name Param Type Return Type boolean isBig(int x) { return x > 10; } @net0pyr / @HendrikEbbers
  25. 25. Karakun DevHub_ dev.karakun.com Methods / Functions function isBig(x: number): boolean { return x > 10; } boolean isBig(int x) { return x > 10; } Param NameParam TypeReturn Type @net0pyr / @HendrikEbbers
  26. 26. Karakun DevHub_ dev.karakun.com Function as a type const f = (n: number): number => n * n; handle(f); • In TypeScript functions are first-class citizens @net0pyr / @HendrikEbbers
  27. 27. Karakun DevHub_ dev.karakun.com Functional Interface @FunctionalInterface public interface Square { int square(int n); } Square f = (n) -> n * n; handle(f); • In Java similar functionality can be created by using functional interfaces @net0pyr / @HendrikEbbers
  28. 28. object oriented programming @net0pyr / @HendrikEbbers
  29. 29. Karakun DevHub_ dev.karakun.com Classes class Animal { move(distance: number = 0) { console.log(`Animal moved ${distance} m.`); } } public class Animal { public void move(int distance) { System.out.println("Animal moved " + distance + "m"); } } @net0pyr / @HendrikEbbers
  30. 30. Karakun DevHub_ dev.karakun.com Access modifiers • You might have noticed the missing access modifier in TypeScript. • If you do not define an access modifier, TypeScript automatically uses the public modifier • Both languages know the public, protected and private modifiers @net0pyr / @HendrikEbbers
  31. 31. Karakun DevHub_ dev.karakun.com Access modifiers • In Java the protected modifier allows access from inherited classes or from within the same package • Since we do not have package structures in TypeScript the protected modifier only allows access from inherited classes @net0pyr / @HendrikEbbers
  32. 32. Karakun DevHub_ dev.karakun.com Interfaces • Both TypeScript and Java support interfaces
 
 
 
 • We can see in the sample that TypeScript has a different approach to handle data access interface Countdown { name: string; start(sec: number): void; } public interface Countdown { String getName(); void setName(String name); void start(long sec); } @net0pyr / @HendrikEbbers
  33. 33. Karakun DevHub_ dev.karakun.com Mutable data class Person { name : string; } public class Person { private String name; public void setName(String n) {this.name = n;} public String getName() {return this.name;} } @net0pyr / @HendrikEbbers
  34. 34. Karakun DevHub_ dev.karakun.com Immutable data class Person { readonly birthday : Date; } public class Person { private final Date birthday; public Person(Date birthday) {this.birthday = birthday;} public Date getBirthday() {return this.birthday;} } @net0pyr / @HendrikEbbers
  35. 35. Karakun DevHub_ dev.karakun.com Easier data access in Java • With Records (JEP 359) Java will contain additional functionality to define data classes in the future • Properties will still be accessed by setter/ getter methods but such methods do not need to be implemented any more. @net0pyr / @HendrikEbbers
  36. 36. Karakun DevHub_ dev.karakun.com Abstraction and Inheritance abstract class Animal { abstract makeSound(): void; } public abstract class Animal { public abstract void makeSound(); } @net0pyr / @HendrikEbbers
  37. 37. Karakun DevHub_ dev.karakun.com Abstraction and Inheritance class Dog extends Animal { makeSound(): void {console.log("WUFF");} } public class Dog extends Animal { public void makeSound() {System.out.println("WUFF");} } @net0pyr / @HendrikEbbers
  38. 38. Karakun DevHub_ dev.karakun.com Generics interface Player<T extends Media> { play(media : T); } public interface Player<T extends Media> { public void play(T media); } @net0pyr / @HendrikEbbers
  39. 39. Karakun DevHub_ dev.karakun.com Reflection • Java provides a powerful reflection API • Reflection can be used to inspect code at runtime • Reflection can be used to modify the runtime behavior Method m = foo.getClass().getMethod("play", String.class); m.invoke(foo, "medley.mp3"); @net0pyr / @HendrikEbbers
  40. 40. Karakun DevHub_ dev.karakun.com Reflection Sample class Demo { public foo: number = 1; } console.log(Reflect.has(demo, "foo")); console.log(Reflect.has(demo, "bar")); @net0pyr / @HendrikEbbers • TypeScript does not provide a stable reflection API
  41. 41. Karakun DevHub_ dev.karakun.com Annotations • Java provides annotations to apply metadata • Annotations in Java can be accessed at compile time or runtime • Annotations in Java are heavily bound to reflections @Singleton public class DatabaseService { } @net0pyr / @HendrikEbbers
  42. 42. Karakun DevHub_ dev.karakun.com Decorators Sample function LogMethod(target: any) { console.log(target); } class Demo { @LogMethod public foo() {} } @net0pyr / @HendrikEbbers
  43. 43. Karakun DevHub_ dev.karakun.com Decorators Sample function LogMethod(target: any) { console.log(target); } class Demo { @LogMethod public foo() {} } Method with same name is called automatically No Concrete annotation definition @net0pyr / @HendrikEbbers
  44. 44. functional programming @net0pyr / @HendrikEbbers
  45. 45. Karakun DevHub_ dev.karakun.com Functional Programming @net0pyr / @HendrikEbbers
  46. 46. Karakun DevHub_ dev.karakun.com Functional-ish Programming @net0pyr / @HendrikEbbers + Libraries + Libraries
  47. 47. Karakun DevHub_ dev.karakun.com Functional-ish Programming @net0pyr / @HendrikEbbers • Immutable data structures • Pure functions • Result depends only on parameters • No side-effects
  48. 48. Karakun DevHub_ dev.karakun.com Immutable objects @net0pyr / @HendrikEbbers interface Person { readonly name: string; } public interface Person { private final String name; ... more Code required }
  49. 49. Karakun DevHub_ dev.karakun.com Readonly ≠ Immutable @net0pyr / @HendrikEbbers function evilRename(person: any) { person.name = "Mailer"; } const p: Person = { name: "Müller" }; evilRename(p); console.log(p.name); prints Mailer
  50. 50. Karakun DevHub_ dev.karakun.com Creating immutable objects @net0pyr / @HendrikEbbers const p1: Person = { name: "Müller" }; const p2: Person = { ...p1, name: "Maier" } final Person p1 = new Person("Müller"); Final Person p2 = p1.withName("Müller"); requires a lot of boilerplate
  51. 51. Karakun DevHub_ dev.karakun.com Immutable collections @net0pyr / @HendrikEbbers const n1: ReadonlyArray<number> = [ 1, 2, 3 ]; const n2: ReadonlyArray<number> = [ ...n1, 4 ]; import io.vavr.collection.Vector; final Vector<Integer> n1 = Vector.ofAll(1, 2, 3); Final Vector<Integer> n2 = n1.append(4);
  52. 52. Karakun DevHub_ dev.karakun.com Pure Functions @net0pyr / @HendrikEbbers Responsibility of the developer VAVR, Java Streams, RxJava Lodash, RxJS
  53. 53. but there’s
 more @net0pyr / @HendrikEbbers
  54. 54. Karakun DevHub_ dev.karakun.com Template Strings @net0pyr / @HendrikEbbers const s = `Hello ${name}! How are you?` Text blocks, rest planned…
  55. 55. Karakun DevHub_ dev.karakun.com Type Alias & Union Types @net0pyr / @HendrikEbbers type Fruit = Apple | Banana | Strawberry; function search(id: number): Person | Error { ... }
  56. 56. Karakun DevHub_ dev.karakun.com Type Alias & Union Types @net0pyr / @HendrikEbbers type Greeting = "Hello" | "Aloha"; let s: Greeting; s = "Hello"; s = "Bonjour"; Does not compile
  57. 57. Karakun DevHub_ dev.karakun.com Strict null checks @net0pyr / @HendrikEbbers let s: String; S = null; console.log(s.length); let s: String | null; s = null; console.log(s.length); Compiler Flag: strictNullChecks Does not compile Does not compile
  58. 58. Karakun DevHub_ dev.karakun.com Strict null checks @net0pyr / @HendrikEbbers ... • Optional • @NonNull, @Nullable
  59. 59. Karakun DevHub_ dev.karakun.com Deconstruction @net0pyr / @HendrikEbbers const { firstName, lastName } = person; console.log(firstName, lastName}; const { lastName: name } = person; console.log(name); const { address: { street } } = person; console.log(street);
  60. 60. Karakun DevHub_ dev.karakun.com Deconstruction @net0pyr / @HendrikEbbers return switch(n) { case IntNode(int i) -> i; case AddNode(Node left, Node right) -> left + right; }; • Will be available for records in a future version
  61. 61. … and they lived happily ever after
  62. 62. Happy End
  63. 63. Karakun@ - Beauty and the Beast: Java Versus TypeScript - Not Dead Yet: Java on the Desktop - Productivity Beyond Failure - JavaFX Real-World Applications - Team Diversity the Successful Way - Rich Client Java: Still Going Strong! Sessions & StickersSocialize dev.karakun.com

×