Primitive Types





Primitive Types
Name Description Examples
byte integer -123, 0, 55, 222
short integer -12345, 0, 5, 6789
int integer -123456789, 0, 5, 123456789
long integer -123456789012345L, 0L, 5L, 123456789012345L
float floating point -1.23f, 0.0f, 5.0f, 1.23f, 123e4f
double floating point -123456789.0, 0.0, 123e4, 123456789.0
boolean Boolean logic true, false
char character 'a', 'A', '?', '\'', '\u092E'


The two Java types you have used so far, byte and int, are both classified as primitive types because they are represented directly by some series of bits in computer memory. A primitive type actually sets aside a number of bytes, based upon the size of the type. Here are the primitive types Java recognizes:

Primitive Types
Name Description Bytes Min Max Examples
byte integer 1 -128 127 -123, 0, 55, 222
short integer 2 -32,768 32,767 -12345, 0, 5, 6789
int integer 4 -231 231-1 -123456789, 0, 5, 123456789
long integer 8 -263 263-1 -123456789012345L, 0L, 5L, 123456789012345L
float floating point 4 complicated complicated -1.23f, 0.0f, 5.0f, 1.23f, 123e4f
double floating point 8 complicated complicated -123456789.0, 0.0, 123e4, 123456789.0
boolean Boolean logic platform-dependent n/a n/a true, false
char character 2 U+0000 U+FFFF 'a', 'A', '?', '\'', '\u092E'

Integer Types

There are four integer types: byte, short, int, and long. They can hold positive or negative whole numbers, but cannot represent fractions of a number. Make sure you don't try to assign a number larger than the maximum or smaller than the minimum supported by the type. By default Java considers an integer literal to be an int unless you place an L or an l after it, making it a long.

Assigning integer values.
final byte foo = 123; //uses 1 byte
final short bar = 123; //uses 2 bytes
final int example = 123456; //uses 4 bytes
final long test = example;  //uses 8 bytes

Floating Point Types

The types float and double are primitive types that allow fractional parts of a number. The fractional part is indicated by a decimal point. The name “float” means floating point, which refers to certain way computers represent the whole and fractional portions of the value in memory. The name “double” means double precision, which indicates that more bytes are used internally and therefore a more precise representation of the fractional part is possible.  By default Java considers a floating-point literal to be an double unless you place an F or an f after it, making it a float.

Assigning floating point values.
final float foo = 1.23f; //uses 4 bytes
final double bar = 123456789.123; //uses 8 bytes

Character Type

The char type is conceptually different than the other types. Technically it holds an integer value; you can assign a byte or a short to it, for example. But the value doesn't simply represent an integer; the value represents a “Unicode code point”, which you will learn about in later lessons. Essentially the integer value held by char represents a letter, digit, or some other symbol, and that's how you usually should think of char; the fact that it stored as an integer value “behind the scenes” is a secondary thought most of the time.

The literal representation of a char is simply the letter or other symbol inside single quote, such as 'G'. The backslash character is special; it represents an escape sequence for entering control characters and other special characters. Popular escape sequences are '\t' for tab, '\n' for a newline character, '\'' for a single quote, and '\\' to represent the escape character itself. You can also use the escape sequence '\uXXXX' (where X is some hexadecimal digit) to insert a single Unicode code point.

Assigning character values.
final char foo = 'A'; //uses 2 bytes
final char newline = '\n'; //uses 2 bytes

Boolean Type

The boolean type represents a simple value of true or false.

Type Casting

When moving a value between variables or using it in expressions, the type of the value can change. When assigning an int value to a long variable, for example, Java will implicitly widen the value. But going in the other direction has the potential of losing information, as a smaller type cannot hold all the values that a larger type can. In that case you must explicitly ask Java to narrow the value by using a cast. An explicit cast is performed by placing the destination type in parenthesis before the value to be cast.

Casting a long to an int.
final int foo = 5;
final long bar = foo; //implicit widening
final int risky = (int)bar; //cast for explicit narrowing; be careful!



Primitive Types
Bytes Bits Signed Min Max
byte 1 |........| (8) yes -128 127
short 2 |........|........| (16) yes -32,768 32,767
int 4 |........|........|........|........| (32) yes -231 231-1
long 8 |........|........|........|........|........|........|........|........| (64) yes -263 263-1
float 4 |.-.......|.-.......|........|........| (32) (1+8+23) yes complicated complicated
double 8 |.-.......|....-....|........|........|........|........|........|........| (64) (1+11+52) yes complicated complicated
boolean platform-dependent platform-dependent n/a n/a n/a
char 2 |........|........| (16) no U+0000 U+FFFF


In the Real World

Think About It

Self Evaluation


Create a program that solves the following problems, printing out the answers. Do not use loops, and use as few literals as possible. Verify your answers with a calculator.

Create a compressed archive of your entire project directory and send it to your teacher as usual.

See Also