JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
기타연산자 종류
삼항 연산자
: 항이 3개인 연산자 (<-> 단항 연산자)
(조건)? 참일때값 : 거짓일때값
ex1.
int age = 18;
System.out.println(age>19? "성인입니다" : "청소년입니다");
출력화면

ex2.
import java.util.Scanner;
public class Traffic_Light {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("신호등 입력 프로그램");
System.out.println("빨간불 : 1, 초록불 : 2, 노란불 : 3");
int sign = sc.nextInt();
String result=sign==1?"정지하세요":sign==2?"출발하세요":"서행하세요";
System.out.println(result);
}
}
출력화면
대입연산자
: 값을 할당할 때 사용하는 연산자
ex.
x=3;
복합대입연산자
: 산술연산자와 대입연산자가 복합적으로 사용된 연산자
[변수][산술연산자][대입연산자][값]
ex1.
x=x+3
-> x+=3;
ex2.
public class ComplexOp {
public static void main(String[] args) {
int x=10;
x=x+10; // x=20
int y=10;
y+=10;
System.out.println(x);
System.out.println(y);
int a=3;
a=a+3;
int b=3;
b+=3;
System.out.println(a);
System.out.println(b);
}
}
출력화면
: x와 y, a와 b는 각각 같은 값을 출력한다
ex3.
public class Assign_Operator {
public static void main(String[] args) {
int a=3;
int b=5;
b=a;
System.out.println(b);
a+=1; // a=a+1;
System.out.println(a);
a/=2; // a=a/2;
System.out.println(a);
a*=a; // a=a*a;
System.out.println(a);
}
}
출력화면
instanceof
: 객체의 타입을 확인하는 연산자
참조변수 instanceof 클래스명
* 클래스 : 객체를 만들기 위한 프레임
ex.
public class Hello {
public static void main(String[] args) {
String s = "Hello";
System.out.println(s instanceof String);
// String s가 자바에서 만들어진 객체니?
int i=3;
System.out.println(Integer.valueOf(i) instanceof Integer);
float f=3.14f;
System.out.println(Float.valueOf(f) instanceof Float);
double d=3.14;
System.out.println(Double.valueOf(d) instanceof Double);
char c='a';
System.out.println(Character.valueOf(c) instanceof Character);
}
}
출력화면
JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63
JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71
JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74
JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78
JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80