티스토리 뷰
기본 자료형 ( Primitive Type )
자바에서 기본 자료형은 반드시 사용하기 전에 선언(Declared)되어야 합니다.
OS에 따라 자료형의 길이가 변하지 않습니다.
비 객체 타입으로 null값을 가질 수 없으며 사용 전 초기화를 해야 합니다.
Type | Byte | Range of Values | |||
부울대수 ( Boolean Type ) | boolean | 1bit ) | true, false | ||
숫 자 |
안 전 한 |
정수형 ( Integer Type ) |
byte | 1 Byte ( 8 bit ) | -2^7 ~ 2^7-1 (-128 ~ 127) |
short | 2 Byte ( 16 bit ) | -2^15 ~ 2^15-1 (-32768 ~ 32767) | |||
int | 4 Byte ( 32 bit ) | -2^31 ~ 2^31-1 (-2147483648 ~ 2147483647) | |||
long | 8 Byte ( 64 bit ) | -2^63 ~ 2^63-1 (-9223372036854775808 ~ 9223372036854775807) | |||
실수형 ( Floating Type ) | float | 4 Byte ( 32 bit ) | 0x0.000002P-126f ~ 0x1.fffffeP+127f | ||
double | 8 Byte ( 64 bit ) | 0x0.0000000000001P-1022 ~ 0x1.fffffffffffffP+1023 | |||
문자 ( Character Type ) | char | 2 Byte ( 16 bit ) | \u0000 ~ \uffff (0 ~ 2^15-1) |
문자 ( Character Type )는 자바에서 unsigned로 동작하는 자료형이고 나머지는 signed ( 부호 )가 있는 자료형 입니다. 즉 1 Byte에서 첫번째 1Bit는 부호 Bit로 사용 합니다. ( 음수, 양수 )
BigInteger : 연산자에는 사용 하지 않음
1. 부울 대수 / 정수형
부울 대수 : 참/거짓 값을 가지는 자료형으로 boolean자료형 이라 합니다.
정수형 : 아리비아 숫자 값을 가질수 있는 자료형을 정수형 자료형이라 하며, long으로 선언 하는 경우 "정수리터럴 + L(l)을 붙혀서 사용 합니다.
모든 자료형은 저장 할 수 있는 범위가 있으며 범위 밖에 있는 값은 기본 int형으로 인식 하여 오류 발생하므로 형변환을 하여야 합니다.
public class PrimitiveTypeBooleanNumeric {
public static void main(String[] args) {
// boolean 자료형은 true/false
boolean isVar = true; // isVar은 boolean 자료형으로 true를 저장
if (isVar) { // isVar의 값이 참이면
isVar = false; // isVar를 거짓으로 저장
}
// 정수 리터럴이 선언한 변수 범위에 있으면 선언한 형으로 인식 하지만
// 범위 밖에 있으면 기본 int형으로 인식 하여 오류 발생 -> 형 변환 필요
byte valueByte = 10; // byte 형 변수 valueByte 에 10 저장
short valueShort = 10; // short 형 변수 valueShort 에 10 저장
int valueInt = 10; // int 형 변수 valueInt 에 10 저장
long valueLong = 10; // long 형 변수 valueLong 에 10 저장
long valueLongL = 10L; // long 형 변수 valueLongL 에 10 저장
// 범위 밖에 값이면 오류 발생 ( 32768는 int로 인식 ) -> 형변환 해야 암
//short valueByteOver = 32768;
short valueByteOver = (short) 32768;
System.out.println(String.format("boolean boolean = %s", isVar)); // false
System.out.println(String.format("byte byte = %s", valueByte)); // 10
System.out.println(String.format("short short = %s", valueByte)); // 10
System.out.println(String.format("int int = %s", valueInt)); // 10
System.out.println(String.format("long long = %s", valueLong)); // 10
System.out.println(String.format("long long L = %s", valueLongL)); // 10
System.out.println(String.format("short 범위밖 = %s", valueByteOver)); //-32768
}
}
2. 실수형
실수형 자료형에서 실수 리터럴은 기본은 double 형 입니다.
소수점( 실수 리터럴 )은 float형에 넣으면 오류 발생하므로 형 반환 필요 합니다.
정수형 리터럴을 값으로 넣으면 자동 변환 됩니다.
float : 실수리터럴 + F ( or f )
public class PrimitiveFloat {
public static void main(String[] args) {
// 실수형 자료형에서 실수 리터럴은 기본이 double 형
// 소수점 (실수 리터럴)은 float형에 넣으면 오류 발생 -> 형반환 필요
// 정수형 리터럴은 자동 변환 됨
float numericLiteralToFloat = 10;
float floatingLiteralToFloat = 10.25F;
// 오류 : 형변환 필요
// float valueFloat = 10.25;
float floatingLiteralToCastingFloat = (float) 10.25;
double floatingLiteralTeDouble = 10.25;
double numericLiteralToDouble = 10;
System.out.println(String.format("float numericLiteralToFloat = %f"
, numericLiteralToFloat)); // 10.000000
System.out.println(String.format("float floatingLiteralToFloat = %f"
, floatingLiteralToFloat)); // 10.250000
System.out.println(String.format("float floatingLiteralToCastingFloat = %f"
, floatingLiteralToCastingFloat)); // 10.250000
System.out.println(String.format("double floatingLiteralTeDouble = %f"
, floatingLiteralTeDouble)); // 10.250000
System.out.println(String.format("double numericLiteralToDouble = %f"
, numericLiteralToDouble)); // 10.000000
}
}
3. 문자형
문자형은 유니코드로 변환 해서 메모리에 저장 합니다.
- 유니코드 : 전 세계의 모든 문자를 컴퓨터에서 일관되게 표현하고 다룰 수 있도록 설계된 산업 표준으로 유니코드 협회(Unicode Consortium)가 제정 합니다.
문자형은 한 문자를 의미하며 문자 ( ‘A’ ), 10진수( 65 ), 2진수 ( 0b1000001 ), 8진수( 00101 ), 16진수( 0x0041 ), 유니코드( ‘\u0041’ )로 저장 합니다.
public class PrimitiveChar {
public static void main(String[] arg) {
char charliteralToChar = 'A';
char charIntegerliteralToChar = '3';
// '10'은 문자 2개로 char 범위 밖으로 오류
// char charIntegerliteralToChar = '10';
char integerliteralToChar = 65;
char bianeyliteralToChar = 0b1000001;
char octalliteralToChar = 00101;
char hexadecimalToChar = 0x0041;
char unicodeToChar = '\u0041';
System.out.println(String.format("문자 = %s"
, charliteralToChar)); // A
System.out.println(String.format("숫자문자 = %s"
, charIntegerliteralToChar)); // 3
System.out.println(String.format("10진수 = %s"
, integerliteralToChar)); // A
System.out.println(String.format("2진수 = %s"
, bianeyliteralToChar)); // A
System.out.println(String.format("8진수 = %s"
, octalliteralToChar)); // A
System.out.println(String.format("16진수 = %s"
, hexadecimalToChar)); // A
System.out.println(String.format("유니코드 = %s"
, unicodeToChar)); // A
}
}
진법 변환은 십진수ToN진수 ( Integer.toXXXXString() ), N진수To 10진수 ( Integer.parserXXX() )사용 해야 합니다.
정수를 이진수로 변환 : Integer.toBinaryString
정수를 8진수로 변환 : Integer.toOctalString
정수를 16진수로 변환 : Integer.toHexString
2진수->10진수 변환 : Integer.parseInt(정수 ,2)
8진수->10진수 변환 : Integer.parseInt(정수 ,8)
16진수->10진수 변환 : Integer.parseInt(정수 ,16)
public class DecimalConversion {
public static void main(String[] args) {
int decomalNum = 12;
String binaryNum = "1100";
String octalNum = "14";
String hexNum = "c";
System.out.println("2진수 변환 = "
+ Integer.toBinaryString(decomalNum)); // 1100
System.out.println("8진수 변환 = "
+ Integer.toOctalString(decomalNum)); // 14
System.out.println("16진수 변환 = "
+ Integer.toHexString(decomalNum)); // c
System.out.println("2진수->10진수 변환 = "
+ Integer.parseInt(binaryNum,2)); // 12
System.out.println("8진수->10진수 변환 = "
+ Integer.parseInt(octalNum,8)); // 12
System.out.println("16진수->10진수 변환 = "
+ Integer.parseInt(hexNum,16)); // 12
}
}
'Java > 기본' 카테고리의 다른 글
012. 자바 : 2차원 배열 (0) | 2022.03.01 |
---|---|
011. 자바 : 참조자료형 : 배열 (0) | 2022.02.27 |
009. 변수 (0) | 2022.02.27 |
008. 콘솔에 출력 하기 (0) | 2022.02.25 |
007. 첫번째 프로그램 (0) | 2022.02.25 |