2025-03-10
Object.toString()
๋ฉ์๋Object
ํด๋์ค๋ฅผ ์์๋ฐ์.toString()
๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ํด๋์ค๋ช
@ํด์์ฝ๋ ํํ๋ก ์ถ๋ ฅ๋จ.toString()
์ ์ค๋ฒ๋ผ์ด๋ฉํด์ผ ํจ.package ch14;
// A ํด๋์ค ์ ์
class A {
int x; // ๋ฉค๋ฒ ๋ณ์ x
int y; // ๋ฉค๋ฒ ๋ณ์ y
// toString() ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ (๊ฐ์ฒด๋ฅผ ๋ฌธ์์ด๋ก ๋ณํ)
@Override
public String toString() {
return "A [x=" + x + ", y=" + y + "]";
}
}
public class C01ObjectMain {
public static void main(String[] args) {
A ob1 = new A(); // A ๊ฐ์ฒด ์์ฑ
System.out.println(ob1); // toString() ์๋ ํธ์ถ
System.out.println(ob1.toString()); // ๋ช
์์ ์ผ๋ก toString() ํธ์ถ
}
}
A [x=0, y=0]
A [x=0, y=0]
equals()
๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉequals()
๋ฉ์๋๋ ==
์ฐ์ฐ์์ ๋์ผํ๊ฒ ๊ฐ์ฒด์ ์ฃผ์๊ฐ์ ๋น๊ต.equals()
๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ผ ํจ.package ch14;
// C02Simple ํด๋์ค ์ ์
class C02Simple {
int n; // ๋ฉค๋ฒ ๋ณ์
// ์์ฑ์
C02Simple(int n) {
this.n = n;
}
// equals() ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ฉ (๊ฐ์ฒด์ ๊ฐ ๋น๊ต)
@Override
public boolean equals(Object obj) {
if (obj instanceof C02Simple) { // obj๊ฐ C02Simple ํ์
์ธ์ง ํ์ธ
C02Simple down = (C02Simple) obj; // ๋ค์ด์บ์คํ
return this.n == down.n; // ๋ ๊ฐ์ฒด์ n ๊ฐ์ด ๊ฐ์์ง ๋น๊ต
}
return false;
}
}
public class C02ObjectMain {
public static void main(String[] args) {
C02Simple ob1 = new C02Simple(10);
C02Simple ob2 = new C02Simple(10);
System.out.println(ob1.equals(ob2)); // true (๊ฐ ๋น๊ต)
}
}
true
equals()
์ hashCode()
ํจ๊ป ์ค๋ฒ๋ผ์ด๋ฉObjects.hash()
๋ฅผ ํ์ฉํ์ฌ hashCode()
๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ.package ch14;
import java.util.Objects;
// C03Simple ํด๋์ค ์ ์
class C03Simple {
int n; // ๋ฉค๋ฒ ๋ณ์
// ์์ฑ์
C03Simple(int n) {
this.n = n;
}
// equals() ์ค๋ฒ๋ผ์ด๋ฉ
@Override
public boolean equals(Object obj) {
if (obj instanceof C03Simple) {
C03Simple down = (C03Simple) obj;
return this.n == down.n;
}
return false;
}
// hashCode() ์ค๋ฒ๋ผ์ด๋ฉ (Objects.hash() ํ์ฉ)
@Override
public int hashCode() {
return Objects.hash(this.n);
}
}
public class C03ObjectMain {
public static void main(String[] args) {
C03Simple ob1 = new C03Simple(1);
System.out.println(ob1.hashCode()); // ํด์์ฝ๋ ์ถ๋ ฅ
}
}
Integer
, Double
๋ฑ)package ch14;
public class C04WrapperMain {
public static void main(String[] args) {
Integer ob1 = Integer.valueOf("100"); // Boxing (๋ฌธ์์ด โ Integer)
int n1 = ob1.intValue(); // UnBoxing (Integer โ int)
Integer ob2 = 100; // Auto-Boxing (๊ธฐ๋ณธ ํ์
โ ๊ฐ์ฒด)
int n2 = ob2; // Auto-Unboxing (๊ฐ์ฒด โ ๊ธฐ๋ณธ ํ์
)
System.out.println(n1 + n2); // 100 + 100 = 200
}
}
SimpleDateFormat
)SimpleDateFormat
์ ํ์ฉํ์ฌ ๋ ์ง๋ฅผ ํฌ๋งท ๋ณ๊ฒฝ (parse()
, format()
์ฌ์ฉ).package ch14;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class C06SimpleDateFormatMain {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.print("YYYY/MM/DD ์
๋ ฅ : ");
String ymd = sc.next(); // ์ฌ์ฉ์ ์
๋ ฅ ๋ฐ๊ธฐ
// ์
๋ ฅ ํฌ๋งท ์ ์
SimpleDateFormat fmtin = new SimpleDateFormat("yyyy/MM/dd");
Date date = fmtin.parse(ymd); // ๋ฌธ์์ด โ Date ๋ณํ
// ์ถ๋ ฅ ํฌ๋งท ์ ์
SimpleDateFormat fmtout = new SimpleDateFormat("yyyy~MM~dd");
System.out.println("๋ณํ๋ ๋ ์ง: " + fmtout.format(date)); // Date โ ๋ฌธ์์ด ๋ณํ
sc.close();
}
}
toString()
์ ์ค๋ฒ๋ผ์ด๋ฉํ๋ฉด ๊ฐ์ฒด ์ ๋ณด๋ฅผ ๋ฌธ์์ด๋ก ์ถ๋ ฅํ ์ ์์.equals()
๋ ๊ธฐ๋ณธ์ ์ผ๋ก ==
๊ณผ ๋์ผํ๋ฏ๋ก, ๊ฐ์ฒด์ ๊ฐ์ ๋น๊ตํ๋ ค๋ฉด ์ค๋ฒ๋ผ์ด๋ฉํด์ผ ํจ.equals()
๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ๋ฉด hashCode()
๋ ํจ๊ป ์ค๋ฒ๋ผ์ด๋ฉํด์ผ ํจ.SimpleDateFormat
๋๋ DateTimeFormatter
๋ฅผ ํ์ฉํ๋ฉด ๋จ.