When to use Long vs long in java?

前端 未结 7 1327
爱一瞬间的悲伤
爱一瞬间的悲伤 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:37

    I don't think there's a single correct answer. A few suggestions:

    • The biggest difference I see between long and Long in this context is that Long may be null. If there's a possibility you might have missing values, the Long object will be helpful as null can indicate missing values. If you're using primitives, you'll have to use some special value to indicate missing, which is probably going to be a mess. Speed or size is not likely to be an issue unless you're planning on making an array of a million of these things and then serializing.

    • My preference for validation logic is to throw some sort of custom ValidationException at the point at which the thing could fail. If you're just creating these things with a constructor, the simplest thing would be just to validate there, e.g.

       public ClientInput(Long userid, Long clientid, Map parameterMap, Long timeout_ms, boolean debug) throws ValidationException {          
      
            if (userid == null) throw new ValidationException("UserId is required"); 
                  ...etc, etc...
      }
      

    Ultimately, the ValidationException is only useful if you can catch it at a point where you can do something useful with it - echo it back to a user or whatever.

提交回复
热议问题