본문 바로가기

old/Programming

JAVA에서 char타입 사칙연산은 어떻게 작동할까?

반응형

결과

코드 결과

char type + int type 유니코드를 반환
char type - int type 에러
int type + char type 유니코드를 반환
int type - char type 에러
char type + char type 유니코드를 반환
char type - char type 유니코드를 반환
char type + String type variable 스트링을 반환
char type + char type + String type variable char 숫자+스트링을 반환
  • string은 Primitive Data Type이 아닙니다. Object 입니다.

Primitive Data Types 자바 api 설명

According to Primitive Data Types Java API

The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. This involves stating the variable's type and name, as you've already seen:

int gear = 1;

Doing so tells your program that a field named "gear" exists, holds numerical data, and has an initial value of "1". A variable's data type determines the values it may contain, plus the operations that may be performed on it. In addition to int, the Java programming language supports seven other primitive data types. A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are:

char타입

자바에서는 char타입은 16비트 유니코드로 저장되며, 가장 작은 수는 '\u0000' (=0)이며, 가장 큰수는 '\uffff' (=65,535) 입니다.

테스트

public class Main {
public static void main(String[] args) {
       String temp="apple";
       char a = 'A';// unicode 'A' is 65
       char b = 'B';// unicode 'B' is 66

       System.out.println(a); //65
       System.out.println(a+0); //65
       System.out.println(a-b); //-1
       System.out.println(a + 5); //70
       System.out.println(5 + a); //70
       System.out.println(5 + a+temp);//70apple
//     System.out.println(a - 5); //error java: bad operand typesfor binary operator '-'
//     System.out.println(5 - a); //error java: bad operand typesfor binary operator '-'
       System.out.println(a + temp.charAt(1)); //177
       System.out.println(temp.charAt(1)+a); //177
       System.out.println(a + a); //130
       System.out.println(a + temp); //Aapple

    }
}
반응형

'old > Programming' 카테고리의 다른 글

lombok intellij에 설치법  (0) 2023.07.16
자바 프로그램의 실행 과정  (0) 2023.07.15
빌더 패턴 Builder Pattern  (0) 2023.07.12
힙 자료구조란  (0) 2023.07.05
Java 메소드 매개변수 참조 변경 불가: 왜 그럴까요?  (0) 2023.07.03