본문 바로가기

Java SE/Data Types

Primitive Data Types

Data Types and Data Structures

Primitive Type

Size

Minimum Value

Maximum Value

Wrapper Type

char

  16-bit  

  Unicode 0

  Unicode 216-1

  Character

byte

  8-bit  

  -27
(-128)

  +27-1
(+127)

  Byte

short

  16-bit  

  -215
(-32,768)

  +215-1
(32,767)

  Short

int

  32-bit  

  -231
(-2,147,483,648)

  +231-1
(2,147,483,647)

  Integer

long

  64-bit  

  -263
(-9,223,372,036,854,775,808)

  +263-1
(9,223,372,036,854,775,807)

  Long

float

  32-bit  

  32-bit IEEE 754 floating-point numbers

  Float

double

  64-bit  

  64-bit IEEE 754 floating-point numbers

  Double

boolean

  1-bit  

  true or false

  Boolean

void

  -----  

  -----  

  -----  

  Void

 


Floating-point

Floating-point numbers are like real numbers in mathematics, for example, 3.14159, -0.000001. Java has two kinds of floating-point numbers: float and double, both stored in IEEE-754 format. The default type when you write a floating-point literal is double.

Java floating-point types

type

Size

Range

Precision

name

bytes

bits

approximate

in decimal digits

float

4

32

+/- 3.4 * 1038

6-7

double

8

64

+/- 1.8 * 10308

16

Limited precision

Because there are only a limited number of bits in each floating-point type, some numbers are inexact, just as the decimal system can not represent some numbers exactly, for example 1/3. The most troublesome of these is that 1/10 can not be represented exactly in binary.

Floating-point literals

There are two types of notation for floating-point numbers. Any of these numbers can be followed by "F" (or "f") to make it a float instead of the default double.

  • Standard (American) notation which is a series of digits for the integer part followed by a decimal point followed by a series of digits for the fraction part. Eg, 3.14159 is a double. A sign (+ or -) may precede the number.
  • Scientific notation which is a standard floating-point literal followed by the letter "E" (or "e") followed by an optionally signed exponent of 10 which is used as a multiplier (ie, how to shift the decimal point). Generally scientific notation is used only for very large or small numbers.

Scientific

Standard

1.2345e5

123450.0

1.2345e+5

123450.0

1.2345e-5

0.000012345

Infinity and NaN

No exceptions are generated by floating-point operations. Instead of an interruption in execution, the result of an operation may be positive infinity, negative infinity, or NaN (not a number). Division by zero or overflow produce infinity. Subtracting two infinities produces a NaN. Use methods in the wrapper classes (Float or Double) to test for these values.



 부동소수 리터럴의 디폴트 데이터 타입 테스트

public class Test
{
 public static void main(String[] args)
 {
  float f = 0.123456789;

  System.out.println(f);
 }
}


Test.java:5: possible loss of precision
found   : double
required: float
                float f = 0.123456789;
                          ^
1 error
계속하려면 아무 키나 누르십시오 . . .




 float 데이터 타입의 정밀도 테스트

public class Test
{
 public static void main(String[] args)
 {
  float f = 0.123456789f;

  System.out.println(f); // 0.12345679
 }
}


0.12345679
계속하려면 아무 키나 누르십시오 . . .



 
double 형의 정밀도 테스트

public class Test
{
 public static void main(String[] args)
 {
  double d = 0.1234567890123456789d;
  // d표시는 생략가능, 부동소수표현은 디폴트로 double형

  System.out.println(d);
 }
}


0.12345678901234568
계속하려면 아무 키나 누르십시오 . . .