Object-2

hashCode()
hashcode=hash(key);
HashCodeTest.java
package object;
public class HashCodeTest{
public static void main(String[] args){
String str1=new String("abc");
String str2=new String("abc");
System.out.println(str1.hashCode());
System.out.println(str2.hashCode());
}
}

96354

96354

EqualsTest.java
package object;
class Student{
int studentId;
String studentName;
public Student(int studentId,String studentName){
this.studentId=studentId;
this.studentName=studentName;
}
public String toString(){
return studentId+","+studentName;
}
@Override
public int hashCode(){
return studentId;
}
}
public class EqualsTest{
public static void main(String[] args){
Student studentLee=new Student(100,"");
Student studentLee2=studentLee;
Student studentSang=new Student(100,"");
if(studentLee==studentLee2)
System.out.println("studentLee studentLee2 .");
else
System.out.println("studentLee studentLee2 .");
if(studentLee.equals(studentLee2))
System.out.println("studentLee studentLee2 .");
else
System.out.println("studentLee studentLee2 .");
if(studentLee==studentSang)
System.out.println("studentLee studentSang .");
else
System.out.println("studentLee studentSang .");
if(studentLee.equals(studentSang))
System.out.println("studentLee studentSang .");
else
System.out.println("studentLee studentSang .");
System.out.println("studentLee hashCode :"+studentLee.hashCode());
System.out.println("studentSang hashCode :"+studentSang.hashCode());
System.out.println("studentLee :"+System.identitiyHashCode(studentLee));
System.out.println("studentSang :"+System.identityHashCode(studentSang));
}
}

studentLee와 studentLee2의 주소는 같습니다.

studentLee와 studentLee2는 동일합니다.

studentLee와 studentSang의 주소는 다릅니다.

studentLee와 studentSang는 동일하지 않습니다.

studentLee의 hashCode :100

studentSang의 hashCode :100

studentLee의 실제 주소값 :225534817

studentSang의 실제 주소값 :1878246837

System.identityHashCode()
실제 주소값을 출력
clone()
in order to make clone of object must implements Cloneable interface
ObjectCloneTest.java
package object;
class Point{
int x;
int y;
Point(int x, int y){
this.x=x;
this.y=y;
}
@Override
public String toString(){
return "x="+x+","+"y="+y;
}
}
class Circle implements Cloneable{
Point point;
int radius;
Circle(int x, int y, int radius){
this.radius=radius;
point=new Point(x,y);
}
@Override
public String toString(){
return " "+point+","+" "+radius+" .";
}
@Override
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
public class ObjectCloneTest{
public static void main(String[] args)throws CloneNotSupportedException{
Circle circle=new Circle(10,20,30);
Circle copyCircle=(Circle)circle.clone();
System.out.println(circle);
System.out.println(copyCircle);
System.out.println(System.identityHashCode(circle));
System.out.println(System.identityHashCode(copyCircle));
}
}

원점은 x=10,y=20이고, 반지름은 30 입니다.

원점은 x=10,y=20이고, 반지름은 30 입니다.

1878246837

929338653