나를 기록하다
article thumbnail
반응형

4. 비교 연산자


비교 연산자도 이항 연산자이므로 연산을 수행하기 전에 형변환을 통해 두 피연산자의 타입을 같게 맞춘 다음 비교 실시

  • [예제] 비교 연산자 자동 형변환ex-2) ‘0’ == 0 → 48 == 0 → falseex-4) ‘A’ > ‘B’ → 65 > 66 → false
  • ex-5) ‘A’ + 1 != ‘B’ → 66 != 66 → false
  • ex-3) ‘A’ == 65 → 65 == 65 → true
  • ex-1) 10 == 10.0f → 10.0f == 10.0f → true

4.1 서로 다른 타입의 비교 연산

  • [예제] 10.0 == 10.0f ? , 0.1 == 0.1f?
public class _14_ComparisonOperator {
    public static void main(String[] args) {
        float f = 0.1f;
        double d = 0.1;
        double d2 = (double)f;

        System.out.printf("10.0==10.0f  %b%n", 10.0==10.0f); // 10.0==10.0f true
        System.out.printf(" 0.1== 0.1f  %b%n", 0.1==0.1f); // 0.1==0.1f
        System.out.printf("f  = %19.17f%n", f);
        System.out.printf("d  = %19.17f%n", d);
        System.out.printf("d2 = %19.17f%n", d2);
    }
}
// 결과
10.0==10.0f  true
 0.1== 0.1f  false
f  = 0.10000000149011612
d  = 0.10000000000000000
d2 = 0.10000000149011612

‘10.0==10.0f’ → true, ‘0.1==0.1f’ → false 인 이유?

→ 10.0f 는 오차없이 저장할 수 있는 값이기에 double 형태로 변환하여도 그대로 10.0

0.1f 는 저장할 때 2진수로 변환하는 과정에서 오차가 발생. double 타입의 상수인 0.1도 저장하는 과정에서 오차가 발생하지만, float 타입의 리터럴인 0.1f보다 적은 오차로 저장된다.

float f = 0.1f; // f에 0.10000000149011612 로 저장된다.

double d = 0.1 ; // d에 0.10000000000000000 로 저장된다.

float 타입의 값을 double로 형변환하면, 부호와 지수는 달라지지 않고 그저 가수의 빈자리를 0으로 채울 뿐

→ 0.1f를 double로 형변환해도 그 값은 전혀 달라지지 않음

→ float 타입의 값을 정밀도가 더 높은 double 타입으로 형변환했다고 해서 오차가 적어지지 않는다!

  • 식 ‘d==f’가 연산되는 과정
    d == f
    → d == (double) f
    → 0.10000000000000000 == (double) 0.10000000149011612 f
    → false

 

4.2 문자열의 비교

  • 두 문자열을 비교할 때는 equals() 사용
  • [예제] 문자열의 비교; equals()
public class _15_StringComparison {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = new String("abc");

        System.out.printf("\\"abc\\"==\\"abc\\" ? %b%n",    "abc"=="abc");
        System.out.printf(" str1==\\"abc\\" ? %b%n",      str1=="abc");
        System.out.printf(" str2==\\"abc\\" ? %b%n",      str2=="abc");
        System.out.printf("str1.equals(\\"abc\\") ? %b%n", str1.equals("abc"));
        System.out.printf("str2.equals(\\"abc\\") ? %b%n", str2.equals("abc"));
        System.out.printf("str2.equals(\\"ABC\\") ? %b%n", str2.equals("ABC"));
        System.out.printf("str2.equalsIgnoreCase(\\"ABC\\") ? %b%n", str2.equalsIgnoreCase("ABC"));
    }
}
// 결과
public class _15_StringComparison {
    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = new String("abc");

        System.out.printf("\\"abc\\"==\\"abc\\" ? %b%n",    "abc"=="abc");
        System.out.printf(" str1==\\"abc\\" ? %b%n",      str1=="abc");
        System.out.printf(" str2==\\"abc\\" ? %b%n",      str2=="abc");
        System.out.printf("str1.equals(\\"abc\\") ? %b%n", str1.equals("abc"));
        System.out.printf("str2.equals(\\"abc\\") ? %b%n", str2.equals("abc"));
        System.out.printf("str2.equals(\\"ABC\\") ? %b%n", str2.equals("ABC"));
        System.out.printf("str2.equalsIgnoreCase(\\"ABC\\") ? %b%n", str2.equalsIgnoreCase("ABC"));
    }
}

str2와 “abc”의 내용이 같은데도 ‘==’로 비교하면 false의 결과를 얻음

→ 내용은 같지만 서로 다른 객체이기 때문!

→ equals()는 객체가 달라도 내용이 같으면 true 반환 → 문자열 비교할 때는 항상 equals()를 사용!

대소문자 구별하지 않고 비교할 때는 equalsIgnoreCase() 사용

반응형
profile

나를 기록하다

@prao

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...