java.lang
ν¨ν€μ§λ μλ°νλ‘κ·Έλλ°μ κ°μ₯ κΈ°λ³Έμ΄ λλ ν΄λμ€λ‘. import
μμ΄ μ¬μ©ν μ μλ€.
λͺ¨λ ν΄λμ€μ μ΅κ³ μ‘°μμΌλ‘, λλ¬Έμ λͺ¨λ ν΄λμ€λ Object ν΄λμ€μ λ©€λ²λ₯Ό λͺ¨λ μ¬μ©ν μ μλ€.
μλ 11κ°μ λ©μλλ₯Ό κ°μ§κ³ μμΌλ©° λ©€λ² λ³μλ μλ€.
Object class method
description
public boolean equals(Object obj)
κ°μ²΄μ λ΄μ©μ΄ κ°μμ§ λΉκ΅
protected void finalize()
κ°μ²΄ μλ©Έ μ μ νΈμΆ(κ°λΉμ§ 컬λ ν°μ μν΄ νΈμΆ)
κ°μ²΄μ ν΄λμ€ μ 보λ₯Ό λ΄κ³ μλ Class μΈμ€ν΄μ€ λ°ν
κ°μ²΄μ ν΄μμ½λλ₯Ό λ°ν
κ°μ²΄λ₯Ό λ¬Έμμ΄λ‘ λ°ν
κ°μ²΄ μμ μ μ¬μ©νλ €κ³ κΈ°λ€λ¦¬λ μ€λ λλ₯Ό νλλ§ κΉ¨μ
κ°μ²΄ μμ μ μ¬μ©νλ €κ³ κΈ°λ€λ¦¬λ μ€λ λλ₯Ό λͺ¨λ κΉ¨μ
λ€λ₯Έ μ€λ λκ° notify()
νΉμ notifyAll()
μ νΈμΆν λκΉμ§ μ§μ λ μκ°λμ λκΈ° μ§μ
μ΄μ€ equals()
, hashCode()
, toString()
λ©μλλ μμ£Ό μ¬μ©λλ€.
equals(Object obj)
Object ν΄λμ€μ equals()
λ©μλλ λ κ°μ²΄μ μ£Όμκ°(=λμΌμ± λΉκ΅)μ λΉκ΅νλ€.
μ»΄ν¨ν° λ©λͺ¨λ¦¬ νΉμ± μ λ κ°μ κ°μ²΄κ° κ°μ μ£Όμκ°μ κ°λ μΌμ μμ§λ§, λ κ° μ΄μμ μ°Έμ‘°λ³μκ° κ°μ μ£Όμκ°μ κ°λ κ²μ κ°λ₯νλ€.
Copy class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class EqualsTest {
public static void main(String[] args) {
Point a = new Point(2, 3);
Point b = new Point(2, 3);
Point c = a; // aμ cλ κ°μ κ°μ²΄λ₯Ό μ°Έμ‘°
// λμΌμ± λΉκ΅(λΉκ΅ λμμ΄ κ°μ μΈμ€ν΄μ€μΈμ§)
System.out.println(a == b); // false
System.out.println(a == c); // true
// λλ±μ± λΉκ΅(λΉκ΅ λμμ΄ κ°μ κ°μ κ°λμ§), νμ§λ§ Point ν΄λμ€λ equals() λ©μλλ₯Ό μ€λ²λΌμ΄λ©νμ§ μμκΈ° λλ¬Έμ λμΌμ± λΉκ΅μ κ°λ€.
System.out.println(a.equals(b)); // false
System.out.println(a.equals(c)); // true
}
}
Point ν΄λμ€λ equals()
λ©μλλ₯Ό μ€λ²λΌμ΄λ©νμ§ μμκΈ° λλ¬Έμ, Object
ν΄λμ€μ λ©μλκ° νΈμΆνκ² λλ©΄μ λ κ°μ²΄μ μ£Όμκ°(=λμΌμ± λΉκ΅)μ λΉκ΅νλ€.
λ§μ½ λ κ°μ²΄μ λ©€λ² λ³μ κ°μ λΉκ΅(=λλ±μ± λΉκ΅)νκ³ μΆλ€λ©΄, equals()
λ©μλλ₯Ό μ€λ²λΌμ΄λ©νμ¬ μ¬μ©ν μ μλ€.
Copy class Point {
int x;
int y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Point point = (Point) o;
return x == point.x && y == point.y;
}
}
public class EqualsTest {
public static void main(String[] args) {
Point a = new Point(2, 3);
Point b = new Point(2, 3);
Point c = a;
// λμΌμ± λΉκ΅(λΉκ΅ λμμ΄ κ°μ μΈμ€ν΄μ€μΈμ§)
System.out.println(a == b); // false
System.out.println(a == c); // true
// λλ±μ± λΉκ΅(λΉκ΅ λμμ΄ κ°μ κ°μ κ°λμ§)
System.out.println(a.equals(b)); // true
System.out.println(a.equals(c)); // true
}
}
hashCode()
ν΄μ±κΈ°λ²μ μ¬μ©λλ hash function
μ ꡬνν κ²μΌλ‘ μ°Ύκ³ μ νλ κ°μ μ
λ ₯νλ©΄, κ·Έ κ°μ΄ μ μ₯λ μ£Όμ(hash code
)λ₯Ό λ°ννλ€.
String ν΄λμ€μ hashCode()
λ©μλλ λ¬Έμμ΄μ λ΄μ©μ μ΄μ©νμ¬ ν΄μμ½λλ₯Ό μμ±νλ€.
Copy public class HashCodeTest {
public static void main(String[] args) {
String s1 = "ogu";
String s2 = "ogu";
System.out.println(s1.hashCode()); // 109981
System.out.println(s2.hashCode()); // 109981
Object o1 = new Object();
Object o2 = new Object();
System.out.println(o1.hashCode()); // 424058530
System.out.println(o2.hashCode()); // 321001045
}
}
hashCode() & equals()
λμΌν κ°μ²΄λ λμΌν λ©λͺ¨λ¦¬ μ£Όμλ₯Ό κ°λλ€λ κ²μ μλ―Ένλ―λ‘, λμΌν κ°μ²΄λ λμΌν ν΄μμ½λλ₯Ό κ°μ ΈμΌνλ€.
νΉν Hashλ₯Ό μ΄μ©ν μλ£κ΅¬μ‘°λ₯Ό μ΄μ©ν λ, κ°μ²΄μ ν΄μμ½λλ₯Ό μ΄μ©νμ¬ μ μ₯νλ―λ‘, equals()
,hashCode()
λ λ©μλλ κ°μ΄ μ€λ²λΌμ΄λ©ν΄μΌ νλ€.
μλ μμμ²λΌ equals()
λ₯Ό μ€λ²λΌμ΄λ©νμ¬ μλλλ‘ λΉκ΅ν΄ μ£Όμμ§λ§, hashCode()
λ₯Ό μ€λ²λΌμ΄λ©νμ§ μμμ μλνμ§ μμ λ¬Έμ κ° λ°μν μ μλ€.
Copy class Animal {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != this.getClass())
return false;
Animal animal = (Animal) obj;
return this.name.equals(animal.getName());
}
}
class Example {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Animal();
animal1.setName("animal");
animal2.setName("animal");
System.out.println(animal1.equals(animal2)); // true
System.out.println(animal1.hashCode()); // 321001045
System.out.println(animal2.hashCode()); // 791452441
Set<Animal> animals = new HashSet<>();
animals.add(animal1);
animals.add(animal2);
System.out.println(animals); // [Animal@2f2c9b19, Animal@13221655]
// hashCode()κ° μΌμΉνμ§ μμμ, κ°μ κ°μ²΄λ‘ μΈμνμ§ μμ μ€λ³΅ μ μ₯λλ€.
}
}
hashCode()
λ₯Ό μ€λ²λΌμ΄λ©νμ¬ name
μ μ΄μ©νμ¬ ν΄μμ½λλ₯Ό μμ±ν΄μ£Όλ©΄, κ°μ κ°μ²΄λ‘ μΈμνμ¬ μ€λ³΅ μ μ₯λμ§ μλλ€.
Copy class Example {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Animal();
Set<Animal> animals = new HashSet<>();
animals.add(animal1);
animals.add(animal2);
System.out.println(animals); // [Animal@1ad9d]
}
}
class Animal {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != this.getClass())
return false;
Animal animal = (Animal) obj;
return this.name.equals(animal.getName());
}
@Override
public int hashCode() {
return this.name.hashCode();
}
}
toString()
toString()
λ©μλλ κ°μ²΄μ μ 보λ₯Ό λ¬Έμμ΄λ‘ λ°ννλ©°, μΌλ°μ μΌλ‘ μΈμ€ν΄μ€λ ν΄λμ€μ λν μ 보 λλ μΈμ€ν΄μ€ λ³μμ κ°μ λ¬Έμμ΄λ‘ λ°ννλ€.
λ³΄ν΅ λ‘κ·Έλ₯Ό λ¨κΈΈ λ, toString()
λ©μλλ₯Ό μ€λ²λΌμ΄λ©νμ¬ μ¬μ©νλ€.
Copy class Example {
public static void main(String[] args) {
Animal animal = new Animal();
animal.setName("ogu");
System.out.println(
animal.toString()); // Animal{name='ogu'}, print λ©μλλ₯Ό νΈμΆνλ©΄ μλμΌλ‘ toString() λ©μλκ° νΈμΆλλ€.
}
}
class Animal {
// ...
@Override
public String toString() {
return "Animal{" + "name='" + name + '\'' + '}';
}
}
clone()
clone()
λ©μλλ κ°μ²΄λ₯Ό 볡μ νμ¬ μλ‘μ΄ κ°μ²΄λ₯Ό μμ±νλ€.
Object ν΄λμ€μ clone()
λ©μλλ μΈμ€ν΄μ€ λ³μμ κ°λ§ 볡μ¬νκΈ° λλ¬Έμ, μΈμ€ν΄μ€ λ³μκ° μ°Έμ‘°νμΌ κ²½μ°, μ°Έμ‘°ν λ³μμ κ° μλ μ°Έμ‘°κ°μ΄ 볡μ¬λλ€.
λ°λΌμ, clone()
λ©μλλ₯Ό μ€λ²λΌμ΄λ©νμ¬ μ°Έμ‘°ν λ³μμ κ°λ 볡μ¬ν΄μ£Όμ΄μΌ νλ€.
Copy class Example {
public static void main(String[] args) {
Animal animal = new Animal();
animal.setName("ogu");
animal.addList(5);
Animal clone = animal.clone();
System.out.println(clone.equals(animal)); // true
System.out.println(clone.getName()); // ogu
System.out.println(clone.getList()); // [5]
clone.addList(9);
System.out.println(clone.getList()); // [5, 9]
System.out.println(animal.getList()); // [5, 9]
}
}
class Animal implements Cloneable {
private String name;
private List<Integer> list = new ArrayList<>();
public List<Integer> getList() {
return list;
}
public void addList(int number) {
list.add(number);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj == this)
return true;
if (obj.getClass() != this.getClass())
return false;
Animal animal = (Animal) obj;
return this.name.equals(animal.name) && this.list.equals(animal.list);
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public Animal clone() {
try {
Animal clone = (Animal) super.clone();
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
μλ‘μ΄ List κ°μ μμ±νμ¬ λ³΅μ¬ν΄μ£Όλ©΄, μ°Έμ‘°ν λ³μμ κ°λ 볡μ¬νλ©΄μ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€.
Copy class Example {
// ...
@Override
public Animal clone() {
try {
Animal clone = (Animal) super.clone();
clone.list = new ArrayList<>(this.list);
return clone;
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
}
getClass()
getClass()
λ©μλλ κ°μ²΄μ ν΄λμ€ μ 보λ₯Ό λ°ννλ€.
Copy class Example {
public static void main(String[] args) {
Animal animal = new Animal();
System.out.println(animal.getClass()); // class Animal
}
}
μ°Έκ³ μλ£
Last updated 2 months ago