java연산자2
1. 비교 연산자
- 비교 연산자는 참 거짓이라는 결과만 나온다.
package operator;
public class Comp1 {
static void main(String[] args) {
int a = 2;
int b = 3;
System.out.println(a == b);
System.out.println(a != b);
System.out.println(a > b);
System.out.println(a < b);
System.out.println(a >= b);
System.out.println(a <= b);
boolean result = a == b;
System.out.println(result);
}
}
- 문자열의 비교는
.equals로 한다
package operator;
public class Comp2 {
static void main(String[] args) {
String str1 = "문자열1";
String str2 = "문자열2";
boolean result1 = "hello".equals("hello");
boolean result2 = str1.equals("문자열1");
boolean result3 = str1.equals(str2);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
}
}
2. 논리 연산자
&&(그리고) : and 연산자||(또는) : or 연산자!(not) : 부정 연산자
package operator;
public class Logical1 {
static void main(String[] args) {
System.out.println("&&: And 연산");
System.out.println(true && true);
System.out.println(true && false);
System.out.println(false && false);
System.out.println("||: Or 연산");
System.out.println(true || true);
System.out.println(true || false);
System.out.println(false || false);
System.out.println("! 연산");
System.out.println(!true);
System.out.println(!false);
System.out.println("변수 활용");
boolean a = true;
boolean b = false;
System.out.println(a && b);
System.out.println(a || b);
System.out.println(!a);
System.out.println(!b);
}
}
package operator;
public class Logical2 {
static void main(String[] args) {
int a = 15;
// a는 10보다 크고 20보다 작다
boolean result = a > 10 && a < 20;
System.out.println("result = " + result);
}
}
3. 대입 연산자
=대입 연산자+=-=*=/=%=축약 대입 연산자
package operator;
public class Assign1 {
static void main(String[] args) {
int a = 5;
a += 3;
a -= 2;
a *= 4;
a /= 3;
a %= 5;
System.out.println(a);
}
}