Table of Contents
Primitive Data Types
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 |
- 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
intmay be 16, 32, or 64 bits, or larger - a
charmay besignedorunsigned
- an
- 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
- Unlike C and C++, where, depending on the platform:
- 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++
- Unlike C and C++, where they are undefined for auto variables.
- 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++
- Unlike C and C++, where you can mix signed and unsigned arithmetic
- 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)
- Unlike C and C++, where char may be:
- 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!)
- Unlike C, which uses integers instead
- Initial/default values of primitive types are well-defined
- 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 \
- Java uses Unicode
Examples of the Use of Primitive Data Types
| Type | Example |
|---|---|
boolean | boolean isOld; |
char | char c = 'A'; |
| Integral | byte by = 45; |
| Floating Point | float across = 45.67f; |
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 Types | Reference 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’.
