| type | 기본값 |
|---|---|
| boolean | false |
| byte | 0 |
| short | 0 |
| int (기본) | 0 |
| long | 0L |
| float | 0.0F |
| double (기본) | 0.0 |
| char | ‘\u0000’ |
모두 null이다.
코드가 어떻게 동작해야하는지에 따라 다르다.
무엇을 사용해야 할지 확신이 없다면, 항상 primitive type을 사용하라.
해답 대신 질문을 이끌어내기
Integer 대신 int를 사용하면 무슨 일이 일어날까?enum 대신 String constant를 사용하면 무슨 일이 일어날까?boolean 대신 Boolean을 사용하면 무슨 일이 일어날까?public class ConsignmentDetail {
private long id;
private Float latitude;
private Float longitude;
private boolean isAssigned;
}
public class OrderDetail {
private long id;
private String name;
private int quantity;
private boolean isActive;
private Float weight;
}
isActive가 null일 수 있을까? 활동적이거나 활발하지 않은 상태만 있을 뿐 또다른 상태는 없다.float를 사용하면 잘못된 결과를 줄 수 있다.public interface OrderManagementService {
Optional<ConsignmentDetail> scheduleOrder(List<OrderDetail> orderDetail, boolean isInternational, int userId);
boolean cancelOrder(int consignmentId);
void updateConsignment(ConsignmentDetail consignment, Integer latitude, Integer longitude) throws Exception;
}
userId는 null이 아닌 유효한 값을 가져야 한다.Database Models
DTOs (Request & Response)
Implementations
public class OrderManagementServiceImpl implements OrderManagementService {
@Override
public Optional<ConsignmentDetail> scheduleOrder(List<OrderDetail> orderDetailList, boolean isInternational, int userId) {
log.info("Generating consignment for user: {}", userId);
if(isInternational) {
ConsignmentDetail consignmentDetail = new ConsignmentDetail();
consignmentDetail.setAssigned(true);
float totalWeight = 0f;
for (OrderDetail orderDetail : orderDetailList) {
totalWeight += orderDetail.getWeight();
}
consignmentDetail.setTotalWeight(totalWeight);
return Optional.of(consignmentDetail);
}else {
return Optional.empty();
}
}
}
NullPointerException이 발생한다.