Contact Us or call 1-877-932-8228


Primitive Data Types

  • The primitive data types store single values at some memory location that the compiler selects and maps to the variable name you declared .
  • Primitive values are not objects - they do not have fields or methods.
  • A primitive value is stored at the named location, while an object is accessed using a reference.
  • An object reference variable does not store the object's data directly - it stores a reference to the block of data, which is somewhere else in memory (technically, the reference stores the memory address of the object, but you never get to see or use the address).
Primitives Data Types
Primitive Type
Storage Size
boolean 1 bit not usable mathematically, but can be used with logical and bitwise operators
char 16 bits unsigned, not usable for math without converting to int
byte 8 bits signed
short 16 bits signed
int 32 bits signed
long 64 bits signed
float 32 bits signed
double 64 bits signed
void None not really a primitive, but worth including here

Object Data Types

Objects can be data, which can be stored in variables, passed to methods, or returned from methods.


As we will see later, objects are stored differently than primitives. An object variable stores a reference to the object (the object is located at some other memory location, and the reference is something like a memory address).

Text Strings

A sequence of text, such as a name, an error message, etc., is known as a string.

In Java, the String class is used to hold a sequence of text characters.

A String object:

  • Is accessed through a reference, since it is an object.
  • Has a number of useful methods, for case-sensitive or case-insensitive comparisons, obtaining substrings, determining the number of characters, converting to upper or lower case, etc.
  • Is immutable; that is, once created it cannot be changed (but you can make your variable reference a different String object at any time).

Literal Values

A value typed into your code is called a literal value.

The compiler makes certain assumptions about literals:

  • true and false are literal boolean values.
  • null is a literal reference to nothing (for objects).
  • A numeric value with no decimal places becomes an int, unless it is immediately assigned into a variable of a smaller type and falls within the valid range for that type.
  • A value with decimal places becomes a double.
  • To store a text character, you can put apostrophes around the character.
char e = 'X'; Creates a 16-bit variable to hold the Unicode value for the uppercase X character.

You can add modifiers to values to instruct the compiler what type of value to create (note that all the modifiers described below can use either uppercase or lowercase letters).

Modifying prefixes enable you to use a different number base:

0X or 0x A base 16 value; the extra digits can be either uppercase or lowercase, as in char c = 0x1b;
0 A base 8 value, as in int i = 0765;
  • Note: using these prefixes will always result in number that is considered positive (so that 0x7F for a byte would be OK, but 0x80 would not, since the latter would have a value of 128, outside the range of byte).
  • Also note that a long value would need the L modifier as discussed below.

Modifying suffixes create a value of a different type than the default:

L or l a long value (uses 64 bits of storage), as in long l = 1234567890123456L;
Note: An int value will always implicitly be promoted to a long when required, but the reverse is not true; the above notation is necessary because the literal value is larger than 32 bits
F or f A float value, as in float f = 3.7F;

Escape Sequences for Character Values

There are a number of escape sequences that are used for special characters:

Escape Sequence
Resulting Character
\b Backspace
\f Form feed
\n Linefeed character - note that it produces exactly one character, Unicode 10 (\u000A in hex)
\r Carriage return
\t Tab
\" Quote mark
\' Apostrophe
\\ Backslash
\uNNNN Unicode value, where N is a base 16 digit from 0 through F; valid values are \u0000 through \uFFFF
\NNN Value expressed in octal; ranging from \000 to \377

The escape sequences can either be used for single characters or within strings of text:

char c = '\u1234';