[javaone 2025] A New Model for Java Object Initialization

Speaker: Dan Smith

See the table of contents for more posts


General

  • Dictionary says initializing is to set something to a starting position/value/configuration
  • Dangerous things happen if don’t satisfy invariants
  • Initialize – local variables, fields, arrays, classes, class instances, specific objects, components, modules, frameworks, systems

Variables

  • Well behaved variable always initialized before used
  • If final, only one initialization
  • Local variables checked at compile time
  • Whether the default value is an initial value depends on programmer intent
  • Final fields are good because must be initialized/can’t be mutated. However don’t prevent reading before initialization. (via constructor calling a method)

Class initialization state

  • Uninitialized – init hasn’t started yet, class can’t be used
  • Larval – init code running in specific thread; other threads block. Can see default values or final values changing. Can see accidental state
  • Initialized – init completed, class can be used
  • Erroneous – init threw exception. Class may never be used

Instance initialization

  • Some code runs early in construction like passing things to another constructor
  • Other code runs later like instance initializers or constructor bodies

JEP 492 – Flexible constructor bodies

  • Allows more code to run in early phase.
  • Can have lines of code before this/super call
  • Larval state now split into early and late larval
  • Early larval – constructor code is running up the hierarchy
  • Late larval – nrolling down the hierarchy. starting from when Object superclass initialized
  • In early larval, can’t use this or invoke instance methods
  • In late larval, be careful. this can be shared; field may change value, final fields may change
  • Instance initializers considered early larval
  • Array creation expressions produced an initialized array. Programmers don’t see a larval state.

Why change?

  • Initialization bugs are subtle and hard to see
  • Value classes must initialize fields before sharing larval object so JVM can freely make copies
  • Allows null checked variables to ensure cannot be null so can’t have a default value

Future

  • Want to change the timing so field initializers and implicit super() run in early phase.

My take

It was like getting a pick behind the scenes. I also learned that larval is a state. (said in Q&A that concept exists now for classes but doesn’t use term. And for instances the concept is there but don’t use the term state) The beginning was slow and I was worried I would be bored. But it got better

[javaone 2025] sneak peak at the stable values API

Speaker: Per Minborg

See the table of contents for more posts


Like Schrödinger’s Cat, find out if immutable when look

General

  • StableValue – class in JDK
  • JEP 502 – stable values. Maybe preview in Java 25
  • Deferred Shallow Immutability.
  • Significant performance gains
  • Codes like lazy initialization; performs like a constant

Motivation

  • Why create object up front (ex: static) if might not need it
  • Singleton pattern needs synchronization to be correct, Showed double checked locking singleton initialization. Easy to get wrong. For example, if you don’t use volatile, can read partially initialized field.
  • With array, volatile doesn’t help becasuse elements aren’t volatile so need method handle to make volatile
  • These solutions are a bunch off code even if you get them right.

StableValue

  • Generic class. Create as empty: private final StableValue<Logger> logger = StableValue.of()
  • Use logger.orElseSet(() -> Logger.create(X.class) – API guarantees this lambda will be only called once
  • StableValue guaranteed to not change value
  • Better to use Supplier: static final Supplier<X> SUPPLIER = StableValue.supplier(X::new) and then just call SUPPLIER.get()
  • StableValue.list(SIZE, _ -> new X()) – immutable list
  • IntFunction<X> FUNC = StableValue.intFunction(size, _ -> new X()) – creates multiple singletons. Pass index to get that singleton to use.
  • Function<Integer, Double> F = StableValue.function(Set.of(1, 2, 3), i -> i*2))
  • StableValue.map(Set.of(1, 2, 3), i -> i*2))
  • List<Integer> LIST = StableValue.list(SIZE, x -> y)

Notes

  • Fibonacci sequence is no longer exponential. Figures out values as need them. Way faster
  • Why called stable and not lazy – because lazy is only one of the properties
  • Don’t have to make static, but faster if do
  • Stable value for record is much faster than for class because guaranteed to be immutable

StableContainer

  • StableContainer COMPONENTS = StableContainer.of(providers…) – this isn’t in the JEP so not sure if this is a local interface

Deferred Immutability

  • Mutable – non final
  • Stable (Stablevalue) – can update anywhere. if two threads updated, the winner sets the value and then becomes constant
  • Immutable – final – can only update in constructor/class initializer

My take

Nice to get a preview into the future. This was not an API I had heard of. Maybe because it isn’t even in preview yet.

[javaone 2025] Jump-Start Your Data Science Learning with Jupyter Notebooks and Java

Speaker Brian Sam-Bodden @bsbodden

See the table of contents for more posts


General

  • At school everything was Python. Then saw had to rewrite to get to production because was a giant notebook.
  • Data science – multi disciplinary field that uses scientific methods, algorithms, processes and systems to extract knowledge and actionable insights from structured/unstructured data
  • Data science – extracts insights/knowledge from data.
  • ML – algorithms capable of learning patterns from data and making predictions
  • Deep learning – specialized methods
  • Foundations – statistics, big data, ML, distributed computing, NLP, Gen AI
  • Present day – Applied AI – RAG evolves, agents are back

Jupyter Notebooks

  • Can run tests against notebooks from CI/CD
  • Spun off iPython project in 2014
  • Browser based notebook interface
  • Supports code, text, math, plots and other media
  • Originally supported Julia, Python and R
  • Code and markdown cells
  • Can replace some documentation with notebooks. Can add button to run in codelabs
  • Can only use one language per notebook; can’t mix and match
  • Important to runs in cell order to avoid errors
  • Good for experimentation and discovery, executable docs, defacto communication medium in the data science/ML/AI community

Build data science stack with Java

  • Java is the dominant force in the enterprise
  • Rich data science ecosystem – DJL weka, Mahout, mallet, flink, H20.ai, semantic-kernal, spark, smile, MLLIb, jenetics and many more libraries
  • DL4J no longer maintained. DJL (Deep Java Library) is different
  • python – pandas – matrix apps, display data, data frame

Java stack

  • Jupyter Lab Docker Stack Image (Notebook is single Notebook. Lab is interface to show notebooks)
  • JJava Jupyter kernel – there are others; this is one of the most stables.
  • Curated set of Java libraries
  • Glue code to streamline API usage

JJava Jupyter Kernel

  • Well maintained
  • Fork of IJava Kernel
  • Uses JShell
  • Java 21
  • Can write code in a cell without any ceremony – ex: just a println Or a full class and then the code to run [like how the java playground at dev.java deals with classes]

Glue code

  • Glue code can be in jar. Don’t have to put all code in notebook.
  • Good to have the methods be static

Example: linear regression with Iris dataset

  • Python version – uses pandas/data frame, showed tables and plots, linear regression class, test vs training data
  • Java version – load dependencies via maven command, DS.read() to get dataframe, DS was a three line glue code method. Then showed tables/plots. Code too long; mostly abstracted
  • JFreeChart to show plot
  • DFLib for linear regression along with commons math
  • Created a linear regression class using commons math

More examples

  • Object detection
  • Visualize embeddings of Vectors
  • Code is very short
  • https://github.com/bsbodden/data-science-with-java

RAG with Spring AI and Redis

  • Redis – very fast vector database, also caching
  • Vectorize question to retrieve
  • Enhance question to augment
  • And then ask LLM for answer

My take

Good intro. Assumed didn’t know anything about notebooks to start. I like that he showed the Iris example in both Python and Java. This was all new to me and great to see. I’m confused about who it is for. I thought the sales pitch of Python was that easier to code for data scientists vs Java devs. Maybe creating a DSL for the data scientists? He said at the end about reusing skillsets.