Primitive Data Types
Home ] Up ] Program Structure & Environment ] Comments ] [ Primitive Data Types ] Reference Data Types ] No Preprocessor ] Typesafe enums ]

 

 

Java has the following primitive data types:

Type

Contains

Default

Size

Minimum
Value

Maximum
Value

boolean true or false false 1 bit Not applicable Not applicable
char Unicode character \u0000 16 bits \u0000 \uFFFF
byte signed integer 0 8 bits -128 127
short signed integer 0 16 bits -32,768 32,767
int signed integer 0 32 bits -2,147,483,648 2,147,483,647
long signed integer 0 64 bits -9,223,372,036,854,775,808 9,223,372,036,854,775,807
float IEEE 754 floating point 0.0 32 bits ±3.40282347E+38 ±1.40239846E-45
double IEEE 754 floating point 0.0 64 bits

±1.79769313486231570E+308

±4.94065645841246544E-324

And here are the corresponding declarations and literals:

Type Declaration Literal
boolean boolean isEmpty; true, false
char char theCharacter; 'A', '\n', '\377', '\u0500'
byte byte theByte; 0, 034, 0xFF
short short theShort;
not short int s;
53, 02333, 0xccff
int int theInt; 745, 034567, 0xDADAFFFF
long long theLong;
not long int v;
1234L, 54607l, 05674l, 0xC080L
float float theFloat; 1e1f, 2.f, .3F, 0f, 3.14f, 6.022e+23f
double double theDouble; 1e1, 2. .3, 0.0, 3.14D, 1e-9d, 1e137
  1. The attributes of the primitive data types were chosen to enhance portability and distributability:
    • Each primitive type has a fixed size, regardless of platform
    • Unlike C and C++, where, depending on the platform:
      • an int may be 16, 32, or 64 bits, or larger
      • a char may be signed or unsigned
    • Each primitive type has fixed semantics, regardless of platform
      • Unlike C and C++, where, depending on the platform:
        • integer datatypes may be 2's-complement, 1's-complement, or whatever
        • floating point datatypes may be IEEE, or other floating point formats.
        • platforms may use big-endian or little-endian semantics
  2. They were also chosen to enhance safety:
    • Initial/default values of primitive types are well-defined
      • Unlike C and C++, where they are undefined for auto variables.
        • Probably the most common cause of bugs in C and C++
    • Java has no unsigned integers and no unsigned keyword
      • Unlike C and C++, where you can mix signed and unsigned arithmetic
        • The source of many subtle bugs in C and C++
    • Java distinguishes between char and byte (and short)
      • Unlike C and C++, where char may be:
        • either a character, or a 'tiny integer'
        • signed or unsigned (the source of subtle bugs, and portability problems)
    • Java strings (including string literals) are true objects (see later)
      • Unlike C and C++, where they are simply arrays of char
    • Java has a boolean type which is not an integer
      • Unlike C, which uses integers instead
        • C programs typically intermix integers and booleans in cavalier fashion
      • C++ now has a bool data type, but it was not in the original C++ proposals.
        • C++ programs typically intermix integers and bools in the same cavalier fashion, due to the lack of a true boolean type in many C++ compilers (also because of programmer laziness!)
  3. They were also chosen to enhance internationalizability:
    • Java uses Unicode
      • for Java characters
      • for Java strings
      • for Java identifiers
    • Unicode:
      • is a 16-bit character encoding
      • encompasses the characters from many different languages
      • the first 128 characters are the same as the ASCII character set
      • the first 256 characters are the same as the ISO8859-1 (Latin-1) character set
      • Java uses all the standard C escapes, like \n, \t, \r, \xxx (where xxx is three octal digits) etc.
      • Java uses \uxxxx (where xxxx is four hex digits)
      • Java does not support line continuation using \

Examples of the Use of Primitive Data Types

Type Example
boolean
boolean isOld;
int age = 52;
...
isOld = (age > 50);
if (isOld)
    PayPension();
char
char c = 'A';
char newline = '\n';
char apostrophe = '\'';
char delete = '\377';
char aleph = '\u0500';
Integral
byte by = 45;
short sh = -323;
int i = 04567;
long lng = 0x08DADL;		
Floating Point
float  across = 45.67f;
double cross  = -123e-23;

 

This page was last modified on 02 October, 2007