⭐알게 된 점⭐
👉 @Data
- @Getter, @Setter, @ToString, @EqualsAndHashCode, @RequiredArgsConstructor 자동 적용
👉 @EqualsAndHashCode
- equals : 두 객체의 내용이 같은지, 동등성을 비교하는 연산자
- hashCode : 두 객체가 같은 객체인지, 동일성을 비교하는 연산자
@ResponseBody
@RequestMapping("/model-attribute-v1")
public String modelAttributeV1(@ModelAttribute HelloData helloData){
HelloData helloData2 = new HelloData();
helloData2.setUsername("hello");
helloData2.setAge(20);
int code1 = helloData.hashCode(); //99166983
int code2 = helloData2.hashCode(); //99166983
System.out.println(helloData.equals(helloData2)); // true
return "ok";
}
- hashData 인스턴스와 helloData2 인스턴스의 인스턴스 주소값은 다르지만 hashCode값은 같음
- hashCode는 객체 주소값이 아닌, 객체 내부의 있는 값만을 다룸!
결론
- hashCode() : 두 객체의 내부 값이 같은지 숫자로 확인
- equals() : 같은 객체인지 확인
🐰 END
영차영차