[Book Review] JAVA의 신 (2)

[Book Review] JAVA의 신 (2)

CH5. 계산

연산자
자료형을 더하거나 빼는 계산을 위해 사용

대입연산자
ㅤ=

산술연산자
ㅤ+
ㅤ-
ㅤ*
ㅤ/
ㅤ%

public class Operator {
  public static void main(String args[]) {
    Operator sampel = new Operator();
    sample.operate();
  }
ㅤ
  public void operate() {
    int intValue1 = 123;
    int intValue2 = 456;
    int result = intValue1 +-*/% intValue2;
    System.out.println(result);
  }
}

복합대입연산자
ㅤ+=
ㅤ-=
ㅤ*=
ㅤ/=
ㅤ%=

단항연산자
ㅤ+ : X1
ㅤ- : X (-1)
ㅤ++
ㅤ--
ㅤ!

++변수 : 변수 참조 전 1더함
변수++ : 변수 참조 후 1더함

계산 우선순위
1) 단항연산자 : ++, --, +, -, !
2) 산술연산자
2-1) *, /, %
2-2) +, -

비교연산자
ㅤ==
ㅤ!=
ㅤ>
ㅤ>=
ㅤ<
ㅤ<=

true / false로 반환

논리연산자
ㅤ&& : AND
ㅤ|| : OR

? 연산자 (삼항연산자)
변수 = (boolean 조건식) ? true일때값 : false일떄값:

자료형 변환
서로다른 타입사이에 변환하는 작업을 하는 것
boolean은 형변환 안됨

casting(형변환)
1) byte 타입으로 할당한 변수를 short 타입의 변수로 지정하고 싶을 때
2) short 타입으로 할당한 변수를 byte 타입의 변수로 지정하고 싶을 때

public class OperatorCasting {
  public static void main(String args[]) {
    OperatorCasting operator = new OperatorCasting();
    operator.casting();
  }
ㅤ
  public void casting() {
    byte byteValue = 127;
    short hortValue=byteValue;
ㅤ
    shortValue++;
    System.out.printl(shortValue);
    byteValue=(byte)shortValue;
    System.out.println(byteValue);
  }
}

bsil / fd 순서는 형변환 문제 없음
short -> byte의 경우 128이 byte로 변하면서 최고차항에 1이 생기며 음수값 -> -128 출력




CH6. 조건

if / else문

if (____ && or || ____)
if((age>25 || isMarried) && height >= 190)
조건연산자 여러 개일 경우 묶어주는 것이 좋음

switch문

public class ControlSwitch{
  public static void main(String args[]) {
    ControlSwitch control = new ControlSwitch();
    control.switchStatement(1);
    control.switchStatement(2);
  }
  public void switchStatement(int numOfWheel) {
    switch(snumOfWheel) {
      case 1:
      case 3:
      case 5:
        System.out.println(numOfWheel + “it’s one”);
        break;
      case 2:
      case 4:
      case 6:
        System.out.println(numOfWheel + “it’s two”);
        break;
      default:
        System.out.println(numOfWheel + “default”);
        break;
  }
}

Java 7부터 String도 switch문에 사용가능

반복문

while(boolean조건) {
처리문장;
if(조건문) break or continue;
}

무한루프 조심하기

do-while문

do {
처리문장;
} while (조건문)

for문

for(초기화; 종료조건; 조건값증가) {
처리문장
}

label (잘 사용하지 않음)
반복문 벗어날 때 특정한 곳으로 이동

public void table() {
  startLabel;
  for(int leve=2;level<10;level++) {
    for(int unit=1;unit<10;unit++) {
      if(unit==4) continue startLabel;
      System.out.print(level+”*”+unit+”=”+level*unit+””);
    }
  System.out.println();
  }
}

좋은 웹페이지 즐겨찾기