Primitive Data Types

Java has the following primitive data types:

TypeContainsDefaultSizeMinimum
Value
Maximum
Value
booleantrue or falsefalse1 bitNot applicableNot applicable
charUnicode character\u000016 bits\u0000\uFFFF
bytesigned integer08 bits-128127
shortsigned integer016 bits-32,76832,767
intsigned integer032 bits-2,147,483,6482,147,483,647
longsigned integer064 bits-9,223,372,036,854,775,8089,223,372,036,854,775,807
floatIEEE 754 floating point0.032 bits±3.40282347E+38±1.40239846E-45
doubleIEEE 754 floating point0.064 bits±1.79769313486231570E+308±4.94065645841246544E-324

And here are the corresponding declarations and literals:

TypeDeclarationLiteral
booleanboolean isEmpty;true, false
charchar theCharacter;‘A’, ‘\n’, ‘\377’, ‘\u0500’
bytebyte theByte;0, 034, 0xFF
shortshort theShort;
not short int s;
53, 02333, 0xccff
intint theInt;745, 034567, 0xDADAFFFF
longlong theLong;
not long int v;
1234L, 54607l, 05674l, 0xC080L
floatfloat theFloat;1e1f, 2.f, .3F, 0f, 3.14f, 6.022e+23f
doubledouble 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
      • 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

TypeExample
booleanboolean isOld;
int age = 52;
...
isOld = (age > 50);
if (isOld)
PayPension();
charchar c = 'A';
char newline = '\n';
char apostrophe = '\'';
char delete = '\377';
char aleph = '\u0500';
Integralbyte by = 45;
short sh = -323;
int i = 04567;
long lng = 0x08DADL;
Floating Pointfloat across = 45.67f;
double cross = -123e-23;

Reference Data Types

The Primitive Data Types are:

boolean, char, byte, short, int, long, float, double

Everything else is a Reference Type

  • Objects
  • Arrays

For example:

Grommet g,h;
g = new Grommet();// g refers to a Grommet object
h = g;            // h refers to the same Grommet object

Major Differences Between Primitive and Reference Types

Primitive TypesReference Types
Can be declared locally within a function.References can be declared locally, but the objects themselves are always newed from the heap.
Assigning one to another copies the value.Assigning one to another adds a new reference to an object.
To copy one object to another, you use the clone() method.
Function arguments passed by value.Function arguments passed by reference. (Arguments are ‘references, passed by value’.)
== and != operators compare values.== and != operators test whether references refer to the same object.
To compare values, you typically use the equals() method.
Each type has a default initial value.Initial value of a reference is null, meaning absence of reference. You can explicitly set a reference to null by assigning it.

Other Points

Java has no pointers!!!

  • This is goodness — believe me!
  • It isn’t as restrictive as you might first think, if you’re coming from C or C++.

You always use the new operator to create an instance of a class (object)

  • There is no delete operator!
  • Java takes care of cleaning up using a technique called garbage collection.

Here’s How References Work:

  • Each assignment adds a new reference to the object.
  • Each assignment from null removes the reference to the object.
  • When the reference count goes to zero, the object is a candidate for garbage collection.

Summary of Reference Types (from Java in a Nutshell, Flanagan)

  • All objects and arrays are handled by reference.
  • The references themselves are passed by value to methods.
  • The = and == operators assign and test references.
  • Use clone() to copy an object
  • Use equals() to test that two different objects ‘are equal’ (have the same value)
  • The referencing and dereferencing is handled automatically by Java.
  • A reference type can never be cast to a primitive type
  • A primitive type can never be cast to a reference type
  • There is no pointer arithmetic in Java
  • There is no sizeof operator in Java
  • null is a special value that means ‘no object’ or ‘absence of reference to object’.