Entity members should they be primitive data types or java data types?

前端 未结 3 844
死守一世寂寞
死守一世寂寞 2021-02-05 03:12

Is there a difference in declaring the enabled variable as Boolean or boolean? Which is preferable from a memory footprint perspective.

@Entity
class User {

            


        
3条回答
  •  既然无缘
    2021-02-05 03:56

    I would usually suggest using the primitive types, just to get rid of null checks all over the place. But it really depends on what you want to say. Your Boolean now can hold 3 values:

    1. true
    2. false
    3. null

    And null can make a total new semantic when dealing with entities. I usually use it as "No data available". Your "enabled" might be a bad example for this kind of field. But lets say you have a number which holds the age of a person.

    private Integer age;
    

    When you use null, you can treat this as: "Unknown". You could also use an int and define a special value (-1) for this case, but null is the more natural solution.

    So, to sum it up. Use primitives if there is always a meaningful value (required fields?) and wrapper classes for optional values.

提交回复
热议问题