등가 비교 연산자
| 연산자 | 연산자 기능 |
|---|---|
| == | 같음 |
| != | 같지 않음 |
아래 코드 결과를 보면 기본 자료형 int, double 과 char와 정수와의 비교연산이 가능한것을 확인 할 수있다.
public class OperationComparision {
public static void main(String[] args) {
OperationComparision operationComparision = new OperationComparision();
operationComparision.comparision();
}
public void comparision() {
char charValue = 'a';
System.out.println(charValue == 97);//true
int intValue = 1;
double doubleValue = 1.0;
System.out.println(intValue == doubleValue);//true
boolean boolT = true;
boolean boolF = false;
System.out.println(boolF == boolT);//false
}
}
대소비교연산자
| 연산자 | 연산자 기능 |
|---|---|
| > | 왼쪽이 큼 |
| >= | 왼쪽 값이 같거나 큼 |
| < | 오른쪽이 큼 |
| <= | 오른쪽값이 같거나 큼 |
Operator '<' cannot be applied to 'boolean', 'boolean’ 경고 메세지를 확인 할 수 있다.=< 이런식으로 순서가 변경되서 작성하면 컴파일 에러를 발생시킨다.boolean boolT = true;
boolean boolF = false;
System.out.println(boolF < boolT);논리 연산자
삼항 연산자
변수 = (boolean조건식) ? true일때 리턴할 값 ? false일때 리턴할 값형변환(casting)
자료형의 범위가 큰 타입 → 작은 타입으로 변환 시에는 (작은 타입)을 명시해야 한다. 명시 하지 않으면 컴파일 에러 발생
boolean타입은 형변환되지 않는다.
아래 코드 예시에서 short → byte로 캐스팅하면서 bit제일 마지막 앞자리(부호처리) 값을 가지게 되었다. 그리하여 -128값이 나오게 되었다.
public class OperatorCasting {
public static void main(String[] args) {
OperatorCasting operatorCasting = new OperatorCasting();
operatorCasting.casting();
}
public void casting() {
byte byteValue = 127;
short shortValue = byteValue;
shortValue++;
System.out.println("shortValue = " + shortValue);//128
byteValue = (byte)shortValue;
System.out.println("byteValue = " + byteValue);//-128
shortValue = byteValue;
System.out.println("shortValue = " + shortValue);//-128
}
}
byte에서 short값으로 캐스팅 되어 short변수에 128이 담겨있다.

byteValue = (byte)shortValue; short type 변수를 byte 타입으로 캐스팅한 결과 8bit만 남게되었다. bit자리의 가장 앞자리에 1이 존재하여 값이 -128이 되었다.

shortValue = byteValue;
범위가 작은 타입에서 범위가 큰 타입으로 변환시는 별도로 형을 명시하지 않아도 형변환이 이루어진다. bsil FD순서로 형 변환 시 아무런 문제가 없이 가능하다.
라고 자바의 신에 나와있는데 byte → short로 캐스팅 시값은 동일하게 -128이 나오는데 bit에는 어떻게 표현되는지 잘 모르겠다.
첫번째 표처럼이면 128이 나와야하는게 맞는거 같고, 아래 표라면 -32768이 나와야할거같은데..

타입별 사용 가능한 연산자
int num = 3 + 4;byteValue = (byte) shortValue;public class SalaryManager {
static double WORK_TAX = 12.5;
static double PENSION = 8.1;
static double INSURANCE = 13.5;
public static void main(String[] args) {
int yearlySalary = 20000000;
System.out.println(getMonthlySalary(yearlySalary));
}
public static double getMonthlySalary(int yearlySalary) {
double month = 12.0;
double monthlySalary = yearlySalary / month;
double totalTax = calculateWorkTax(monthlySalary);
totalTax += calculateNationalPension(monthlySalary);
totalTax += calculateHealthInsurance(monthlySalary);
System.out.println("totalTax = " + totalTax);
return monthlySalary - totalTax;
}
/**
* 근로소득세 계산
* @param monthSalary
* @return
*/
public static double calculateWorkTax(double monthSalary) {
return monthSalary * (WORK_TAX/100);
}
/**
* 국민연금 계산
* @param monthSalary
* @return
*/
public static double calculateNationalPension(double monthSalary) {
return monthSalary * (PENSION/100);
}
/**
* 건보료 계산
* @param monthSalary
* @return
*/
public static double calculateHealthInsurance(double monthSalary) {
return monthSalary * (INSURANCE/100);
}
}