Will the jit optimize new objects

前端 未结 5 1434
北荒
北荒 2020-11-30 13:27

I created this class for being immutable and having a fluent API:

public final class Message {
    public final String email;
    public final String escalat         


        
5条回答
  •  一向
    一向 (楼主)
    2020-11-30 13:53

    will the optimizer be able to avoid lots of object creation

    No, but instantiation is a very cheap operation on the JVM. Worrying about this performance loss would be a typical example of premature optimization.

    Is there anything to be gained from making a separate mutable builder object instead?

    Working with immutables is generally a good approach. On the other hand builders also won't hurt you, if you use the builder instances in a small context, so their mutable state is accessible only in a small, local envorironment. I don't see any severe disadvantages on any side, it is really up to your preference.

提交回复
热议问题