[Programming]/[Language]

[Java] Ch.3 연산자 / K-Digital Credit Java / Java 기초 / Summary

김파고띠 2022. 3. 19. 13:58

 

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

 

// 연산자 - 01 대입, 부호, 산술, 복합대입. 증감연산자

 

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

 

// 부호 연산자

int num1 = -10;

int num2 = 20;

 

System.out.println(+num1);

System.out.println(+num2);

 

System.out.println(-num1);

System.out.println(-num2);

 

//산술 연산자

int num3 = 10%4;

System.out.println(num3);

 

//증가,감소 연산자

int score = 10;

System.out.println(++score);

System.out.println(score++);

System.out.println(score);

System.out.println(--score);

System.out.println(score--);

System.out.println(score);

 

}

}

 

/*

항과 연산자

  • 항(=Operand) : 연산에 사용되는 값
  • 연산자(=Operator) : 항을 이용하여 연산하는 기호
  • 단항 연산자 / 이항 연산자 / 삼항 연산자

종류

  • 대입 연산자 : 왼쪽 변수에 오른쪽 값 대입 / 우선 순위 가장 낮은 연산자

Ex) int age = 20;

  • 부호 연산자 : 단항 연산자 / 변수의 부호를 유지(+) 혹 바꿈(-) /

변수 값 변하려면 대입연산자 이용해야 함

  • 산술 연산자 : 사칙연산 연산자
  • 복합 대입 연산자 : 대입 연산자와 다른 연산자가 함께 쓰임

Ex) += / -= / *= / /= / &=

Ex) <<= / >>= / >>>= / &= / |= / ^=

  • 증가/감소 연산자 : 변수의 값을 1을 더하거나,뺄 때 사용 / 단항 연산자



*/

 

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

 

// 연산자 - 02 관계, 논리, 조건, 비트 연산자

 

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

 

// 단락 회로 평가 유의점에 대한 예시

int num = 10;

int i = 2;

boolean value = ( ( num = num + 10 ) < 10 ) && ( ( i = i+2 ) < 10 );

System.out.println(num);

System.out.println(i);

System.out.println(value);

 

//조건 연산자

int num1 = 10;

int num2 = 14;

int max = (num1>num2)? num1:num2;

 

System.out.println(max);

 

}

}

 

/*

관계 연산자: 연산의 결과가 true/false로 반환 / 비교 연산자라고도 함

Ex) > / < / >= / <= / == / !=

논리 연산자 : 연산의 결과가 true/false로 반환 / 관계 연산자와 함께 많이 사용됨

Ex) &&(논리곱) / ||(논리합) / !(부정)

단락 회로 평가(=Short Circuit Evaluation)란?

논리 곱과 논리 항은 앞의 항에 결과에 따라 뒤를 연산하지 않을수도 있는 것

실제 프로그램에서 예상하지 않는 결과가 발생할 수 있음으로 유의

조건 연산자: 삼항 연산자 / 식 = 조건식? 결과 1 : 결과2;

Ex) int num = (5>3)? 10;20;

 

*/

 

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

 

// 연산자 - 03 관계, 논리, 조건, 비트 연산자

 

import java.util.*;

import java.lang.*;

import java.io.*;

 

class Codechef

{

public static void main (String[] args) throws java.lang.Exception

{

 

//비트 연산자 이용 예시

 

int num1 = 0B00001010; //10

int num2 = 0B00000101; //5

 

System.out.println( num1 & num2 );

System.out.println( num1 | num2 );

System.out.println( num1 ^ num2 );

 

System.out.println( num1 >> 3 );

System.out.println( num1 << 3 );



}

}

 

/*

비트 연산자: 대입연산자와 다른 연산자가 함께 쓰임

마스크 : 특정 비트를 가리고 몇 개의 비트 값만 사용할 때

비트 켜기 : 특정 비트들만을 1로 설정해서 사용하고 싶을 때

예) &00001111 (하위 4비트 중 1인 비트만 꺼내기)

비트 끄기 : 특정 비트들만을 0으로 설정해서 사용하고 싶을 때

예) 11110000 ( 하위 4비트 중 0인 비트만 0으로 만들기 )

비트 토글 : 모든 비트들을 0은 1로, 1은 0으로 바꾸고 싶을 때

*/

 

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

—-----------—-----------—-----------—-----------—-----------—-----------—-----------—-----------

 

이 정리 내용은 패스트 캠퍼스, K-Digital Credit Java 강의를 참고했습니다