Object class, equals()
✅ Object class
Object classis parent of all Java classes- highest parent of all Java class
- 자바의 최상위 부모 클래스
- implicitly imports
object public class Parent extends Object(extends Object는 생략)child class extends ParentParent class extends Object- when
child classis created, bothparent classandobject classis created
- ✔️ Object class methods
toString()equals()getClass()
✅ Object and polymorphism
Object classis parent of all Java classes- parent class can become child class
- thus,
Object classcan become all class
1
2
3
4
5
6
7
8
9
10
11
12
Dog dog = new Dog();
Car car = new Car();
dog.toString(); //child can call parent method
Object objDog = new Dog(); //부모는 자식을 담을 수 있다, upcasting
Object objCar = new Car();
objDog.dogSound(); //🔴 compile error, parent does not know child class field and method
if(obj instanceof Dog dog){
dog.dogSound(); //🟢 after downcasting Obj -> dog, then can call child method
}
✅ Object and array
Object classis parent of all Java classesObject arraycan contain all types of classes
1
2
3
4
5
Dog dog = new Dog();
Car car = new Car();
Object object = new Object();
Object[] objectArrays = {dog, car, object};
💡 toString();
- every class can call
toString()
1
2
3
4
5
6
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
//💡 result
com.example.javaintermediate1.lang.toString.Car@7344699f
Integer.toHexString(hashCode(): 우리가 보는 객체의 참조값hashCode(): 객체의 참조값toHexString: 16진수 숫자문자로 바꾸기- able to override
toString()method in individual class
1
2
3
4
5
6
7
8
9
10
11
public class Dog{
@Override
public String toString() {
return "Dog{" +
"dogName='" + dogName + '\'' +
", age=" + age +
'}';
}
}
//💡 result
Dog{dogName='poppy1 ', age=2}
✅ Object and OCP
- open for adding new class, for example
new Car, new Dog - closed in modifying old class for example,
Object class - without modifying
Object class,CarandDogcan useSysytem.out.println() - ➡️ Polymorphism(
extends Objectandmethod overriding) - ➡️ OCP
- Thus,
Object classis a huge example of polymorphism and OCP
💡 equals();
==: is it pointing to the same instance?equals(): has the same value?Object equals()기본 메소드는 어떤 기준으로 비교해야 하는지 모른다- (회원 아이디가 같으면 같은 회원인가? 회원 주민등록번호가 같으면 같은 회원인가?)
- ➡️
equals()method override
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
User userA = new User("123");
User userB = new User("123");
userA == userB //false
// 👎🏻 equals() override 전
// object class의 기본 equals()는 무엇을 기준으로 같은 회원으로 판단해야 하는지 모름
// return flase
userA.equals(userB) //false
//👍🏻 equals() override 후
//회원 아이디가 같으면 같은 회원으로 판단하라고 override 했음
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UserV2 userV2)) return false;
return Objects.equals(id, userV2.id);
}
userA.equals(userB) //true
💡 == and equals() 심화
✔️ primitive int
1
2
3
4
5
int a = 5;
int b = 5;
a == b; //true
a.equals(b) //do not exist
- primitive type에서
==는 compare values - primitive type에서
equals()는 존재하지 않는다
✔️ -128 ~ 127 Reference type Integer, Wrapper
1
2
3
4
5
Integer a = 5;
Integer b = 5;
a == b; // true (cached values between -128 and 127)
a.equals(b); // true
- Normally, in reference type
==checks if they refer to the same object in memory - so it seems
a == bshould be false… - ⚠️ HOWEVER, Java has optimization called Integer cache
- JVM reuses Integer objects for all values in the range of
-128to127 so, both
aandbpoint to the same cached Integer object,5- ⚠️ JVM only caches numeric weapper types,
- Byte
- Short
- Integer
- Long
- Character (for small characters)
- and their cache ranges are typically -128 to 127
✔️ Reference type Integer, Wrapper outside cache range
1
2
3
4
5
Integer a = 128;
Integer b = 128;
a == b; // false (different objects)
a.equals(b); // true (same value)
✔️ Wrapper class that is not cached
1
2
3
4
5
Double a = 10.0;
Double b = 10.0;
a == b; // false
a.equals(b); // true
✔️ Compare primitive and wrapper
1
2
3
4
double a = 10.0;
Double b = 10.0;
x == y; //true
- true bc Java
unboxestheDoubleintoprimitive doublefor comparison
✔️ String
1
2
3
4
5
String a = "hello";
String b = "hello";
a == b; // true
a.equals(b); // true
✔️ ==
- normally, in reference,
==compares reference, whether they refer to the same object in memory - ⚠️ HOWEVER,
string literalsin Java are saved in string pool - 그래서
String이 똑같으면 string pool에 한번만 저장함 - 따라서
a == bistrue
✔️ equals()
- String class overrides .equals() to compare the content (characters) of the string
✔️ String Instance
1
2
3
4
5
String a = new String("hello");
String b = new String("hello");
a == b; // false (different objects)
a.equals(b); // true (same content)
✅
✅
✅
✅
This post is licensed under CC BY 4.0 by the author.
