2016-06-06 3 views
1

때문에 나는 다음과 같은 동작의 내 프로젝트에서 버그를 발견했습니다왜이 Groovy Integer 비교가 false를 반환합니까?

int a = 1 
Integer b = 2 
assert a.class == b.class // ok, they are the same class 
assert Integer.class != int.class // what?! they are different! 

누군가가 왜, 어떻게이 문제를 처리하는 방법을 알고? 내가 링크 및 @Nathan 의견 @alfasin 사용 그루비 2.3.7 감사

+1

정수는 클래스이며 int는 원시 데이터 형식입니다. –

+2

[여기] (http://groovy-lang.org/objectorientation.html)에 대한 좋은 설명이 있습니다. – alfasin

+2

'println (a.class)'을 평가한다면 무엇이 당신에게 나타날 것이라고 생각합니까? –

답변

0

을 사용하고

는, 나는 a가 정수로 (오토 박싱을) autocoverted되고 있음을 발견했습니다. 내 특정 문제에 a.class == b.class하지만 Integer.class != int.class

내가 clazz.getDeclaredFields()를 사용하여 필드의 클래스를 얻기 위해 반사를 사용하고 있습니다 이유

. 클래스의 값은 int이지만 비교해야하는 값은 Integer입니다.

import org.apache.commons.lang.ClassUtils 
ClassUtils.primitiveToWrapper(int.class) == Integer.class 

감사합니다 모두 :

내가 나를 위해 정수로 INT 변환 아래 도우미 클래스를 사용하고이 문제를 해결하려면!

+1

또는'@ CompileStatic' 또는'@ TypeChecked'를 사용하십시오. –

관련 문제