728x90
비교 연산자
비교 연산자
프로그래밍에서 비교란 주어진 값들이 같은지, 다른지, 큰지, 작은지를 구분하는 것을 의미
비교 연산자의 결과는 true(참) 나 false(거짓) 둘 중 하나의 값을 가지게 된다.
*Boolean
불린(Boolean)은 참과 거짓을 의미하는 데이터 타입
정수나 문자와 같이 하나의 데이터 타입으로 참을 의미한 true와 거짓을 의미하는 false 두가지 값을 가지고 있다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package com.doubles.javastudy.basic; public class JavaOperator2 { public static void main(String[] args){ // 비교연산자(==) System.out.println(1==2); // false System.out.println(1==1); // true System.out.println("one"=="two"); // false System.out.println("one"=="one"); // true // 비교연산자(!=) System.out.println(1!=2); // true System.out.println(1!=1); // false System.out.println("one"!="two"); // true System.out.println("one"!="one"); // false // 비교연산자(>, <) System.out.println(10>20); // false System.out.println(10>2); // true System.out.println(10>10); // false System.out.println(10<20); // true System.out.println(10<2); // false System.out.println(10<10); // false // 비교연산자(>=, <=) System.out.println(10>=20); // false System.out.println(10>=2); // true System.out.println(10>=10); // true System.out.println(10<=20); // true System.out.println(10<=2); // false System.out.println(10<=10); // true // * .equals(문자열비교 메서드) String a = "Hello world"; String b = new String("Hello world"); System.out.println(a==b); // false /*== 은 두개의 데이터타입이 동일한 객체인지 알아내기 위한 연산자이므로 서로 다른 객체이기 때문에 false를 리턴하게된다.*/ System.out.println(a.equals(b)); // true /*equals는 서로 다른 객체들간의 값이 같은지 비교할 때 사용 문자와 문자를 비교할 때는 ==를 사용하지 않고 equals를 사용*/ } } | cs |