Java - 연산자
연산자
public class Sample {
public static void main(String[] args) {
//증감연산자
int x = 1;
System.out.println("x=" + x);
x= x+2;
System.out.println("x=" + x);
x++; // x= x+1;
System.out.println("x=" + x);
x +=2; // x=x+2
System.out.println("x=" + x);
// 산술연산자(+,-,*,/,%)
int a = 10;
int b =3;
int c = a%b;
System.out.println("c=" + c);
// 문자열 연결 연산자
String hello= "hello" +"world";
System.out.println(hello);
//비교연산자(>,>=,<,<=,==,!=)
int num1=5;
int num2=10;
boolean result = num1!=num2;
System.out.println("result="+result);
//논리연산자( &&, ||, ! )
result = !result;
result = (num1>5) || (num2>5);
System.out.println("result="+result);
boolean b5 = (a>5) && (b>5);
System.out.println(" b5 : "+ b5);
boolean b6 = (a>5) || (b>5);
System.out.println(" b6 : "+ b6);
boolean b7 = !(a>5) || (b>5);
System.out.println(" b7 : "+ b7);
}
}
++ 추가
삼항연산자 새로 알던 개념이라서 추가적으로 설명을 적어본다.
변수 = ( 조건문 ) ? 피연산자1 : 피연산자2
삼항연산자는 위의 문법을 가진다.
- 조건식의 연산결과가 true 이면 결과는 피연산자 1이고,
- 조건식의 연산결과가 false 이면 결과는 피연산자 2이다.
댓글남기기