Java : Operations on primitive types

m_ngyeong·2023년 12월 13일
0

Java

목록 보기
5/6
post-thumbnail

☕️ Java

Arithmetic operations(산술 연산)

Binary arithmetic operators

연산자는 두 개의 값을 피연산자로 취하므로 이항 연산자라고 한다.

  • addition +
  • subtraction -
  • multiplication *
  • division /
  • remainder %
System.out.println(13 + 25); // 38
System.out.println(30 - 70); // -40
System.out.println(21 * 3);  // 63
System.out.println(8 / 3); // 2
System.out.println(5 % 9); // 5

Unary operators

단항 연산자는 단일 값을 피연산자로 사용하며 곱셉과 나눗셈보다 우선순위가 높다.

  • unary plus operator +
  • unary minus operator -

The precedence order

괄호 > 단항 +/- > *,/,% > +, -

Integer types and operations

Integer types

  • int
  • long : 숫자가 문자 L 또는 l로 끝나면, long으로 간주되고 그렇지 않으면 int로 간주 됨
    문자 l과 숫자 1이 비슷하여 대문자 사용을 권장함
long one = 1L;
long twentyTwo = 22L; // L or l is a literal for longs
long bigNumber = 100_000_000_000L;

long result = bigNumber + twentyTwo - one; 
System.out.println(result); // 100000000021

the assignment operator(할당 연산자)

+=, *=, /=...

int n = 10;
n = n + 4; // 14

int n = 10;
n += 4; // 14

표준입력에서 숫자 읽기

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int a = scanner.nextInt();
        long b = scanner.nextLong();

        long sum = a + b;

        System.out.println(sum);
        
    }
}

Boolean type(True or False) and operations

Boolean type

Boolean type 논리 값을 나타내는 데 사용되며 true/false 두 가지 값만 가지는 데이터 유형이다. 이는 논리 유형이라고도 한다.

Logical operators

  • NOT : !, boolean 값을 반대로 바꾸는 단항 연산자
boolean f = false; // f is false
boolean t = !f;    // t is true
  • AND : &&, 두 피연산자가 모두 true이면 true 반환하고, 그렇지 않으면 false를 반환하는 이항 연산자
boolean b1 = true && true;   // true 
boolean b2 = true && false;  // false
boolean b3 = false && true;  // false
boolean b4 = false && false; // false
  • OR : ||, 하나 이상의 피연산자가 true이면 true를 반환하고, 그렇지 않으면 false를 반환하는 이항 연산자
boolean b1 = true || true;   // true 
boolean b2 = true || false;  // true
boolean b3 = false || true;  // true
boolean b4 = false || false; // false
  • XOR(exclusive OR) : ^, 피연산자의 값이 다르면 true를 반환하고, 그렇지 않으면 false를 반환하는 이항 연산자
boolean b1 = true ^ true;   // false
boolean b2 = true ^ false;  // true
boolean b3 = false ^ true;  // true
boolean b4 = false ^ false; // false

The precedence of logical operators

!(NOT) > ^(XOR) > &&(AND) > ||(OR)

Comparing values: Relational operators

Relational operators

피연산자의 유형에 상관없이 관계 연산자를 적용한 결과는 true/false이며, 관계 연산자는 산술 연산자보다 우선순위가 낮다.

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • >= (greater than or equal to)
  • < (less than)
  • <= (less than or equal to)

Java에서는 a <= b <= c 와 같은 표현식을 작성할 수 없음으로 논리 연산자와 관계 연산자를 결합하여 사용해야 한다.

import java.util.Scanner;

public class CheckDescOrder {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int h1 = scanner.nextInt();
        int h2 = scanner.nextInt();
        int h3 = scanner.nextInt();

        boolean descOrdered = (h1 >= h2) && (h2 >= h3);

        System.out.println(descOrdered);
    }
}


참고문헌,
https://hyperskill.org/tracks/8

profile
사용자 경험 향상과 지속적인 성장을 추구하는 프론트엔드 개발자 ʚȉɞ

0개의 댓글