언어 Language/자바 Java

Java의 변수variables, 데이터타입datatype

Tap to restart 2020. 10. 18. 22:00
반응형

변수(變數) variables

변수는 변하는 수다.

수학 시간에 공부하는 그 변수다. 그 변수를 영어로 variable이라고 한다.

프로그래밍에서는 a = 1 이런 식으로 주로 쓴다.

이 말은 삼성전자가 만드는 그 메모리, RAM에 공간을 만들고 그 공간의 이름을 a라고 부르겠다는 것이고, a란 메모리 공간에 1이란 값을 저장해달란 뜻이다. 프로그래밍에서 변수란 메모리 공간을 뜻한다고 볼 수 있다.

a = 2라고 쓰면 1이란 값은 날리고 2로 새로 저장해 놓는다. a를 출력하라고 명령하면 2가 나온다.

a = "Hello World"

a = 3.141592

a = 10의 100제곱

a = 1 / 3

a = 참

 

이런 저런 정보를 저장하고 싶을 것이다. 여기서 질문이 생기게 된다.

a란 변수에 저렇게 다양한 값을 저장할 수 있을까?

a란 변수의 메모리공간은 얼마나 될까?

메모리는 삼성전자가 만드는 그 메모리로 컴퓨터에 따라 8GB, 16GB 이렇게 크기에 한계가 있다.

무조건 막 넣을 수 없다.

a란 변수에 월 정보 1~12만 저장할 계획인데 1억 이상 숫자를 저장할 수 있는 메모리 공간을 만든다면 메모리 낭비다. 낭비가 쌓이면 프로그램이 느리게 작동하게 되고 사용자들은 짜증이 나게 될 거다.

이때 필요한 개념이 데이터타입이다.

 

자료형, 데이터타입 datatype

각 프로그래밍 언어마다 자료형에 대한 설명이 나온다. 자료형마다 저장할 수 있는 자료의 한계도 나와 있다.

보통 변수를 선언할 때 자료형 정보를 함께 선언하도록 한다. 그래야 메모리를 절약할 수 있으니까.

물론 변수에 아무 자료형이나 저장할 수 있는 프로그래밍 언어도 있다. 자바스크립트처럼.

 

자바는 변수를 만들 때(선언할 때) 자료형도 함께 적어야 한다. 많은 언어들이 마찬가지다. 그래서 새로운 언어를 공부하면 자료형 이름부터 살펴보고 외우게 된다.

 

자바의 자료형

(출처: Java Documentation Primitive Data Types)

 

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.

short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -2^31 and a maximum value of 2^31-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -2^63 and a maximum value of 2^63-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 2^64-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

 

자바 언어 문서에 들어가면 나오는 설명이 위 설명이다.

위 설명을 표로 정리해보면 아래와 같다.

 

자료형 비트 저장 가능 범위
byte 8비트 정수 -128 ~ 127
short 16비트 정수 -32,768 ~ 32,767
int 32비트 정수 -2의 31제곱 ~ 2의 31제곱 - 1
부호 없을 경우 0 ~ 2의 32제곱 - 1
long 64비트 정수 -2의 63제곱 ~ 2의 63제곱 - 1
부호 없을 경우 0 ~ 2의 64제곱 - 1
float 32비트 실수
double 64비트 실수
boolean 1비트 true, false
char 16비트 16비트 유니코드 캐릭터

 

1비트는 0과 1이다. 2가지를 표현할 수 있다. 8비트는 비트가 8개 이므로 2의 8제곱 = 256이다.

그래서 byte가 -128 부터 127까지 가능한 것이다. -128, 0, 127 다 더하면 256이다.

8bit비트 = 1byte바이트, 1024byte바이트 = 1kilobyte킬로바이트,  1024kilobyte킬로바이트 = 1megabyte메가바이트다.

컴퓨터는 0과 1의 세계이므로 1000meter미터 = 1kilometer킬로미터처럼 1000으로 나눠서 표기할 수 없다.

2의 10제곱인 1024로 나눠서 표기한다. 1000의 근사값이다.

 

당연한 소리지만 byte로 변수를 선언하고 저장 가능 범위를 넘어서는 숫자를 저장하려고 하면 에러가 난다.

 

자바에서 변수 선언하고 값 저장하기

byte a = 1;

위 처럼 선언할 수 있다.

자료형 변수명 = 값 세미콜론(;)

형태다. 쌍반점(세미콜론)을 꼭 적어야 한다. 자바에서 모든 선언문 다음에 표시해줘야 한다. 자바란 프로그래밍 언어는 세미콜론을 구분점으로 문장을 쪼개서 처리하나보구나라고 생각하면 된다.

자바한테는

byte a = 1; int b = 3;

이 한 문장이 아니라 두 문장인 거다.

 

값을 바꿀 때는 아래처럼 변수명 적고 값을 적으면 된다.

a = 3;
변수명 = 값 세미콜론(;)

 

여기서 질문이 생길 수 있다. 변수명만 봐서는 자료형이 뭔지 알 수가 없는 것 아닌가.

맞다. 알 수 없다. 프로그램 내용(보통 소스 코드라고 한다)이 길어지면 변수명만 봐서는 어떤 자료형으로 선언했는지 알 수 없다. 그래서 찰스 시모니란 사람이 헝가리안 표기법을 만들게 되었다. 아주 간단한 아이디어다. 변수를 선언할 때 자료형을 앞쪽에 접두어 형태로 적는 방법이다. 예)bMonth (b = byte). 역시 초창기에 뛰어 들어야 한다. 초창기에는 아주 간단한 아이디어만으로도 이름을 역사에 남길 수 있으니. 그밖에도 여러 표기법이 있다.

 

변수 선언 시 주의사항

long은 끝에 L을 붙여줘야 한다. 그래야 int가 아니구나 이해할 수 있기 때문이다.

float은 f 를, double은 d를 붙여줘야 한다. 붙여주지 않으면 float인지 double인지 자바 입장에서 구분하기 어렵기 때문이다.

 

System.out.println이 뭔지 모르겠지만 그냥 출력하는 거구나 이해하자.

byte bA = 127;

System.out.println("bA:" + bA);

라고 적으면 

bA:bA에 저장된 값

출력된다.

결과는

bA:127

이다.

실행 코드 예

class Main {
    public static void main(String[] args) {

        byte bA = 127; 
        byte bB = -128; 
        short sA = 32767;
        short sB = -32768;
        int iA = 2147483647;
        int iB = -2147483648;
        long lA = 9223372036854775807L;
        long lB = -9223372036854775808L;
        float fA = 3.40282347E+38f;
        float fB = -3.40282347E+38f;
        double dA = 1.79769313486231570E308d;
        double dB = -1.79769313486231570E308d;
        boolean boolA = true;
        boolean boolB = false;
        char cA = '\u0000';
        char cB = '\uffff';
        
        System.out.println("bA:" + bA);
        System.out.println("bB:" + bB);
        System.out.println("sA:" + sA);
        System.out.println("sB:" + sB);
        System.out.println("iA:" + iA);
        System.out.println("iB:" + iB);
        System.out.println("lA:" + lA);
        System.out.println("lB:" + lB);
        System.out.println("fA:" + fA);
        System.out.println("fB:" + fB);
        System.out.println("dA:" + dA);
        System.out.println("dB:" + dB);
        System.out.println("boolA:" + boolA);
        System.out.println("boolB:" + boolB);
        System.out.println("cA:" + cA);
        System.out.println("cB:" + cB);
        
        System.out.println(Byte.MIN_VALUE);
        System.out.println(Byte.MAX_VALUE);
        System.out.println(Short.MIN_VALUE);
        System.out.println(Short.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);
        System.out.println(Long.MAX_VALUE);
    }
}

 

코드와 결과 보기

 

에러 내보기

위 링크를 눌러서 코드를 약간 수정해 보자.

127을 128로 바꾸자.

아래와 같은 에러 코드가 뜬다.

Main.java:4: error: incompatible types: possible lossy conversion from int to byte byte bA = 128;

자료형이 잘못되었다는 뜻이다.


iA = 2147483647; 에서 2147483647를 2147483648로 바꾸자.

Main.java:8: error: integer number too large: 2147483648 int iA = 2147483648;

너무 크다고 또 에러가 뜬다.

반응형