두 개의 대상의 데이터 혹은 주소값을 비교하고 싶을 때,
어떤 상황에서 어떤걸 사용하는게 더 정확할까?🤔
true, 다르면 false의 boolean값을 리턴하는 메서드.public class equalTest {
    public static class Student {
		
        // 멤버변수
		String name;
        int studentNum;
        
		// 생성자
        public Student (String name, int studentNum) {
            this.name = name;
            this.studentNum = studentNum;
        }
    }
    public static void main(String[] args) {
        Student st1 = new Student("kim", 111);
        Student st2 = new Student("Lee", 222);
        Student st3 = st1;
		Student st4= new Student("kim", 111);
        System.out.println(st1.equals(st2));   // false
        System.out.println(st1.equals(st3));   // true
		System.out.println(st1.equals(st4));   // false
    }
}
@Override
public boolean equals(Object obj) {
	// 비교대상이 Student형이면
	if (obj instanceof Student) { 
	Student std = (Student)obj;
    if(this.studentNum == std.studentNum)
		// 학번이 같으면 같은 사람
		return true; 
    else
		return false;
 	 }
	return false;
}
public static void main(String[] args) {
	Student st1 = new Student("kim", 111);
	Student st2 = new Student("Lee", 222);
	Student st3 = st1;
	Student st4= new Student("kim", 111);
	
	System.out.println(st1.equals(st2));   // false
	System.out.println(st1.equals(st3));   // true
	System.out.println(st1.equals(st4));   // true
}
public static void main(String[] args) {
	String str1 = "abc";
	String str2 = "abc";
	System.out.println(str1.equals(str2));  // true
}
public static void main(String[] args) {
	Integer num1 = 100;
	Integer num2 = 100;
	System.out.println(num1.equals(num2));  // true
}
String str1 = "apple";                      // 리터럴
String str2 = new String("banana");         // new 연산자

String str1 = "apple";
String str2 = new String("banana");
String str3 = str1;
String str4 = "apple";
System.out.println(str1 == str2);	  // false
System.out.println(str1 == str3);	  // true
System.out.println(str1 == str4);	  // true
String s1 = "apple";
String s2 = new String("apple");
if(s1 == s2) {
    System.out.println("두개의 값이 같음.");
} else {
    System.out.println("두개의 값이 같지 않음.");
}
String s1 = "apple";
String s2 = new String("apple");
if(s1.equals(s2)) {
    System.out.println("두개의 값이 같음.");
} else {
    System.out.println("두개의 값이 같지 않음.");
}
이게 참~~또 은근 헷갈려서 알아보기 쉽게 포스팅을 해보았다ㅎㅎㅎ
포스팅을 하며 정리하다보니 확실히 좀 더 자세하게 깨닫게 되는 듯~~
앞으로는 문자열 자체만 비교할 때는 equals() 메서드를 사용하도록 하자😎