1. 산술 연산자 : * / % + - ++ --
증가연산자, 감소연산자: 1씩 증가(감소)
전위연산자: ++i 증가 후 대입
후위연산자: i++ 대입 후 증가
2. 비교 연산자 : >, <, >=, <=, ==, != =======> true, false
3. 논리 연산자 : &&, ||, ! <- 단락 회로 연산자
& | <- 비트 연산자
논리연산자에 대한 예
class OperatorTest
{
public static void main(String[] args)
{
//int i = 10, j = 5;
//System.out.println(i != j);
int i = 6, j = 7;
//System.out.println(i && j); // 논리값만 연산 가능
//System.out.println(i>5 && j <=7);
//System.out.println(i & j); // 비트 논리 연산
//System.out.println(i >5 & j <= 7);
System.out.println(i>10 && ++j < 9);
System.out.println(i + "\t" + j);
}
}
* 캐스팅 연산자 - 명시적 변환, 암시적 변환
(강제형변환 연산자)
int i;
double d = 3.14;
i = (int) d; <- 강제형 변환 연산자
정수형 기본으로 int
실수형 기본으로 double
class OperatorTest
{
public static void main(String[] args)
{
//int i = 10, j = 5;
//System.out.println(i != j);
//int i = 6, j = 7;
//System.out.println(i && j); // 논리값만 연산 가능
//System.out.println(i>5 && j <=7);
//System.out.println(i & j); // 비트 논리 연산
//System.out.println(i >5 & j <= 7);
//System.out.println(i>10 && ++j < 9);
//System.out.println(i + "\t" + j);
double x, y;
x = 10.0;
y = 3.0;
//int i = (int)x/(int)y;
int i = (int)(x/y);
System.out.println(i);
System.out.println();
y = i;
System.out.println(y);
i = 299;
byte b = (byte)i;
System.out.println("b="+b);
b = (byte)( b + 3);
System.out.println("b="+b);
b += 3;
System.out.println("b="+b);
float f = 3.14f; //float f = (float)3.14;
System.out.println("f="+f);
f = f + 1.5f;
System.out.println("f + 1.5 = " +f);
f += 1.5;
System.out.println("f + 1.5 = " +f);
java.text.DecimalFormat format = new java.text.DecimalFormat("#.###"); // 소수점 자리수 조정을 위해
double d = 3.14, e = 0.0;
e = d + 2.5;
System.out.println("e : " + e);
String str = format.format(e);
System.out.println("e : " + str);
}
}
ChatGPT, 블록체인, 자바, 맥북, 인터넷, 컴퓨터 정보를 공유합니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!