문제> 변수 obj1과 obj2가 java.lang.Object 객체에 대한 인스턴스라고 할때,

만일, (ob1 == ob2가 false리턴) obj1과 ob2가 다르다면, obj1.equals(obj2)의 리턴값이 true가 될수 있을까요?


원본내용>

Suppose ob1 and ob2 are references to instances of java.lang.Object. If (ob1 = = ob2) is false, can ob1.equals(ob2) ever be true?
Please give Explanation also.








출처:
http://www.javalobby.org/java/forums/t54339.html

 

먼저, 출처는 위와 같습니다.

이곳에서 나왔던 질문인데.. 답변역시 몇가지가 있어서 소개합니다.


답변: 1)

http://www.javalobby.org/java/forums/m91954596.html


Object a = new String("a");
Object b = new String("a");
 
System.out.println(a==b);             // false
System.out.println(a.equals(b));   // true

String overides Object.equals(Object obj) to return true if obj is a String object that represents the same sequence of characters.
a and b are both references to java.lang.Object


 

답변: 2)

http://www.javalobby.org/java/forums/m91954985.html

Yes. The equals method compares the objects' fields while == compares the objects' addresses in memory.

좀 의역하면 equals 메소드는 객체의 필드들(보통 저장된 값)을 비교하지만, == 는 객체가 가리키는 메모리상의 주소값을 비교합니다.

 

답변: 3)

http://www.javalobby.org/java/forums/m91955462.html

That's kind of a trick question. Their answer is correct because they are talking about actual instances of type java.lang.Object. It just so happens that the default implementation of java.lang.Object.equals(Object) does an == check. If you create your own class and override the equals method, then it is possible to create a class that when instantiated may return true on it's equals method but not when doing an == check. A good example of this is mentioned in the original response.

 

의역하면, 이문제는 약간의 트릭이 존재합니다. 만일, 그들이 단순히 java.lang.Object 타입에 대한 인스턴스라고 가정하면 문제의 답은 true 일것 입니다. 왜냐하면, java.lang.Object 내의 equals 메소드의 기본적인 구현은 == 연산자 체크를 통해 이루어 지고 있기 때문입니다. 하지만, 만일 당신이 새로운 클래스를 하나 만들고 equals 메소드를 오버라이딩(재정의) 했다면 == 연산값이 false 인경우에도 equals 메소드는 ture을 리턴할수 있습니다.

 

크리에이티브 커먼즈 라이센스
Creative Commons License
Writer profile
author image
Posted by Mr.용

트랙백 주소 :: http://frok.info/man/trackback/129

댓글을 달아 주세요

  1. lesbian pee 2008/03/12 07:44  댓글주소  수정/삭제  댓글쓰기

    친구는 너의 위치의 현재 팬이 되었다! 친구는 너의 위치의 현재 팬이 되었다!

  2. hand right ring womens 2008/03/13 05:38  댓글주소  수정/삭제  댓글쓰기

    너는 아름다운 웹사이트가 있는다!

  3. cock long sex 2008/03/13 05:55  댓글주소  수정/삭제  댓글쓰기

    친구는 너의 위치의 현재 팬이 되었다!

  4. serialz2000 2008/03/13 06:25  댓글주소  수정/삭제  댓글쓰기

    친구는 너의 위치의 현재 팬이 되었다!

  5. hot water heating radiator 2008/03/13 06:51  댓글주소  수정/삭제  댓글쓰기

    위치에 중대한 일은 그것을 좋아했다!

  6. com teen titans 2008/03/13 07:13  댓글주소  수정/삭제  댓글쓰기

    우수한 일! 감사!

  7. bate papo brazil 2008/03/13 07:33  댓글주소  수정/삭제  댓글쓰기

    너는 우수한 위치가 있는다!

  8. gainesville erotic 2008/05/23 04:40  댓글주소  수정/삭제  댓글쓰기

    저에서 유사한 역사는 이었다.

  9. madden girl krash 2008/05/23 04:58  댓글주소  수정/삭제  댓글쓰기

    나는 배웠다 매우…

  10. boy free pic twinks 2008/05/23 05:41  댓글주소  수정/삭제  댓글쓰기

    좋은 영역! 걸출한 영역!

  11. ciaras sex change 2008/05/23 06:06  댓글주소  수정/삭제  댓글쓰기

    유용한 정보. 좋은 디자인.

  12. asian hair pussy 2008/05/23 07:15  댓글주소  수정/삭제  댓글쓰기

    걸출한 블로그!

  13. animal lovers 2008/05/23 07:40  댓글주소  수정/삭제  댓글쓰기

    너는 아주 보는 좋은 위치가 있는다!

  14. moonlight lady anime download 2008/05/24 00:51  댓글주소  수정/삭제  댓글쓰기

    나의 너의 친구는 위치의 현재 팬이 되었다!

  15. femdom fisting 2008/05/24 00:51  댓글주소  수정/삭제  댓글쓰기

    친구는 위치의 너의 현재 팬이 되었다!

  16. manhattan gay massage 2008/05/24 00:56  댓글주소  수정/삭제  댓글쓰기

    나의 너의 친구는 위치의 현재 팬이 되었다!

  17. hot hollywood actress 2008/05/24 01:06  댓글주소  수정/삭제  댓글쓰기

    우수한 위치! 많은 감사.

  18. leather open front tendon boots 2008/05/24 03:49  댓글주소  수정/삭제  댓글쓰기

    많은 감사 위치! 우수한 나는 너의.

  19. moms market 2008/05/24 04:06  댓글주소  수정/삭제  댓글쓰기

    유용한 정보. 좋은 디자인.