Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GeeCon - Breaking Java Stereotypes:It’s Not You...

GeeCon - Breaking Java Stereotypes:It’s Not Your Dad’s Language Anymore

Avatar for A N M Bazlur Rahman

A N M Bazlur Rahman

May 15, 2025
Tweet

More Decks by A N M Bazlur Rahman

Transcript

  1. Bazlur Rahman Java Champion 🏆 Staff Software Developer at DNAstack

    Breaking Java Stereotypes It’s NOT Your Dad’s Language Anymore 1
  2. public class Main { public static void main(String[] args) {

    System.out.println("Hello GeeCon 2025!"); } } When was the last time you wrote... • Modern Java might surprise you • Today's journey: Discovering how Java has evolved 3
  3. Java Stereotypes: What People Think Java is • Verbose and

    boilerplate-heavy • Slow and memory-hungry • Enterprise-only, not for modern applications • Unchanging and stagnant • Losing ground to newer languages • Not relevant in the AI revolution 4
  4. Today's Reality: What Modern Java Actually Is • Concise and

    expressive • High-performance with optimized JIT and GC • Full-stack and cloud-native • Rapidly evolving (6-month release cycle) ◦ Project Amber - improving language feature ◦ Loom- virtual threads ◦ Panama - foreign function and memory ◦ Valhalla - value object, enhancing generics ◦ Leyden - improving startup ◦ Babylon -extending Java's reach to foreign programming models • Growing ecosystem and community • A critical player in AI infrastructure and deployment • 🔄 Incremental, backwards‑compatible upgrades 5
  5. Modern Language Features • Instance Main Methods • Virtual Threads

    (JDK 21) • Records & var inference • Pattern Matching & Sealed Classes • Text Blocks, Enhanced NPEs • Streams, HttpClient, JShell • Foreign Function & Memory API 6
  6. Instance Main Methods // Traditional "Hello, World!" public class HelloWorld

    { public static void main(String[] args) { System.out.println("Hello, World!"); } } // "Hello, World!" using JEP 512 features void main() { IO.println("Hello, World!"); } 7
  7. Virtual Threads try (var exec = Executors.newVirtualThreadPerTaskExecutor()) { IntStream.range(0, 10_000).forEach(i

    -> exec.submit(() -> { Thread.sleep(Duration.ofMillis(500)); return i; }) ); } 8
  8. Records Replace Boilerplate // Old Java public class User {

    private final String name; private final String email; private final int age; public User(String name, String email, int age) { this.name = name; this.email = email; this.age = age; } // Getters, equals, hashCode, toString - 50+ lines of code } // Modern Java record User(String name, String email, int age) { } 9
  9. Pattern Matching • Simplify common programming patterns, enhancing code readability

    and safety. • Works in conditional contexts such as instanceof and switch if (x instanceof String) { String str = (String) x; // use str } if (x instanceof String str) { // use str } 10
  10. Pattern Matching (cont.) if (x instanceof String str && str.length()

    > 3) { // use str } else { // you do something else. } switch (x) { case String str when str.length() > 3 -> { // use str } case Integer n when n < 0 -> { System.out.println("value is zero or lower"); } default -> { // you do something else. } } 11
  11. Pattern Matching Switch String desc = switch (shape) { case

    Circle c -> "Circle radius: " + c.r(); case Square s -> "Square side: " + s.s(); }; 12
  12. Text Blocks – Multi-line Strings Made Easy String json =

    "{\n" + " \"name\": \"Java\",\n" + " \"age\": 28,\n" + " \"traits\": [\"modern\", \"fast\", \"evolving\"]\n" + "}"; String json = """ { "name": "Java", "age": 30, "traits": ["modern", "fast", "evolving"] } """; 14
  13. Controlled Inheritance with Sealed Class sealed interface Option<T> permits Some,

    None {} record Some<T>(T value) implements Option<T> {} record None<T>() implements Option<T> {} String getOptionValue(Option<String> str) { return switch (str) { case None<String> _ -> ""; case Some<String>(var value) -> "the value is %s".formatted(value); }; } 15
  14. Traversing algebraic data types sealed interface Expr permits ConstantExpr, NegExpr,

    PlusExpr, TimesExpr { static int eval(Expr expr) { return switch (expr) { case ConstantExpr(var i) -> i; case NegExpr(var i) -> -eval(i); case PlusExpr(var a, var b) -> eval(a) + eval(b); case TimesExpr(var a, var b) -> eval(a) * eval(b); }; } } record ConstantExpr(int i) implements Expr {} record PlusExpr(Expr a, Expr b) implements Expr {} record TimesExpr(Expr a, Expr b) implements Expr {} record NegExpr(Expr a) implements Expr {} void main(){ IO.println(Expr.eval(new PlusExpr(new ConstantExpr(5), new ConstantExpr(20)))); } It took 19 lines of code, which would have taken 60 lines using traditional Java. 16
  15. Panama FFM Call to native cos() Linker linker = Linker.nativeLinker();

    var cos = linker.downcallHandle( linker.defaultLookup().find("cos").get(), FunctionDescriptor.of(C_DOUBLE, C_DOUBLE) ); double v = (double) cos.invoke(1.0); 17
  16. Developer Experience - JShell and More • Shell (Java 9+):

    Interactive REPL for immediate code testing, API exploration, and learning, eliminating the need for full class boilerplate for simple tasks. • Single-File Execution (Java 11+): Run .java files directly (java Filename.java) without a separate compilation step, enabling Java for scripting. • Expanded Standard Library: More built-in utility methods (e.g., String.strip(), Files.writeString(), new HttpClient in Java 11) reduce dependency on external libraries. • Modern IDEs (IntelliJ, VSCode, Eclipse) offer robust support for new Java features. • Build tools (Maven/Gradle) effectively manage modular JDKs and multi-release JARs. 18
  17. Java vs Modern Languages • Kotlin: Similar conciseness; Java catches

    up on records/var • Go: Virtual threads neutralize goroutine advantage • Rust: Higher control, but Java faster dev & mature GC • Takeaway: Java now competitive on productivity & performance 20
  18. Java AI Initiative • Panama (FFM API): Faster, safer native

    library & foreign memory interaction; optimal off-heap, zero-copy (e.g., Llama 2). • Panama (Vector API): Explicit SIMD for optimal CPU use & high-performance "number crunching"; boosts ML performance (e.g., Llama 2). • Valhalla (Value Classes): Targets optimal on-heap memory & support for new number types (e.g., float16). • Babylon (Code Reflection): Runtime code access; enables interop with foreign models (GPUs, ONNX, auto-diff), e.g., Onyx Script. • Future Vision: "All AI companies grow up to be Java companies." 21
  19. Takeaways • Modern Java = concise, performant, evolving fast •

    Ecosystem depth + new features → strong choice today • Upgrade incrementally—no rewrite needed • Java has truly shed its legacy stereotype 22
  20. Breaking Java Stereotypes:It’s Not Your Dad’s Language Anymore • Mentors,

    writes, speaks at conferences, and contributes to open-source projects outside of regular work hours. • Founder and current moderator of the Java User Group in Bangladesh (since 2013) • Recognized as the Most Valuable Blogger (MVP) at DZone, a leading technology publisher. • Serves as an editor for the Java Queue at InfoQ and at Foojay.io, a community for OpenJDK enthusiasts. • Authored five bestselling books about the Java programming language in Bengali. https://x.com/bazlur_rahman rokon12 https://www.linkedin.com/in/bazlur/ https://bazlur.ca/ https://bsky.app/profile/bazlur.ca 24