Java Primitive Types

  Return to the Java Programming Corner.


Introduction

Not everything in Java is an object. There is a special group of data types (also known as primitive types) that will be used quite often in your programming. For performance reasons, the designers of the Java language decided to include these primitive types.

Creating an object using new isn't very efficient because new will place objects on the heap. This approach would be very costly for small and simple variables. Instead of create variables using new, Java can use primitive types to create automatic variables that are not references. The variables hold the value, and it's place on the stack so its much more efficient.

Java determines the size of each primitive type. These sizes do not change from one machine architecture to another (as do in most other languages). This is one of the key features of the language that makes Java so portable.

Take note that all numeric types are signed. (No unsigned types).

Finally, notice that each primitive data type also has a "wrapper" class defined for it. This means that you can create a "nonprimitive object" (using the wrapper class) on the heap (just like any other object) to represent the particular primitive type.

For example:
int i = 5;
Integer I = new Integer(i);
OR
Integer I = new Integer(5);

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     -128   +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