When to use Long vs long in java?

前端 未结 7 1307
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 13:03

Below is my Interface -

public interface IDBClient {
    public String read(ClientInput input);
}

This is my Implementation of the Interfa

7条回答
  •  自闭症患者
    2020-12-24 13:33

    1 Long is the object orientated counter part of long. The difference is as follows, and it applies to Float to float, Integer to integer etc.

    • long is a primitive type, while Long is a Java class (and so it will inherit Object).
    • long must be assigned with a valid number, while Long can be null
    • long instances can't use the benefits of OO, while instances of Long are real Java objects
    • Long is a serializable so it will be very useful when doing file, database or network IO
    • long is more efficient than Long considering memory space and processing speed

    If you are doing heavy calculations, use primitive types. Otherwise if you're concerning more about design, the object counter parts will be very useful.

    2 Since you are not using any frameworks if I'm observing correctly, I suggest you make an interface like Validated with a method bool validate(). And every time you try to put a input into the database call validate in advance.

提交回复
热议问题