Java Primitive Variables
Java classifies data with data types based on its value.
Because Java is a static language, all variables have types that are set when the variable is declared and once set, the variable’s type cannot be changed.
Data types can be categorized as either primitives or objects. Primitives are stored in the fastest possible memory, directly on the stack and could be numbers, characters or boolean values. Primitives type names are lowercase.
byte
- The byte data type is an 8-bit signed two’s complement integer between -128 (-2^7) and 127 (inclusive)(2^7 -1)
- The default value is 0
- Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an integer.
byte byteVariable = 100;
int
- Int data type is a 32-bit signed two’s complement integer between -2,147,483,648 and 2,147,483,647.
- The default value is 0.
- Used as a default data type for integral values unless there is a concern about memory.
int integer = 1;
char
- Used to store any character
- A character is a single 16-bit Unicode character between ‘\u0000’ (or 0) and ‘\uffff’ (or 65,535 inclusive)
char character = 'n';
short
- Short data type is a 16-bit signed two’s complement integer between -32,768 (-2^15) and 32,767 (inclusive) (2^15 -1)
- Can also be used to save memory as byte data type. A short is 2 times smaller than an integer
- The default value is 0.
short value = -20000;
long
- Long data type is a 64-bit signed two’s complement integer between -9,223,372,036,854,775,808(-2^63) and 9,223,372,036,854,775,807 (inclusive)(2^63 -1)
- This type is used when a wider range than int is needed
- Default value is 0L
long longVariable = 100000L;
double
- Used as the default data type for decimal values
- The double data type is a double-precision 64-bit IEEE 754 floating point
- Double data type should never be used for precise values such as currency
- Default value is 0.0d
double doubleVariable = 123.4
float
- Float is a single-precision 32-bit IEEE 754 floating point
- Used to save memory in large arrays of floating point numbers
- Default value is 0.0f
- Float data type is never used for precise values such as currency
float floatVariable = 234.5f
Inferred Data Types
From Java 10, local variables can use a type inference derived from the assigned value.

Explicit types can be set to numeric literals with alpha notation.
var intVar = 5; var floatVar = 5f; var doubleVar = 5d; var longVar = 5L;
Primitive wrapper classes
There are wrapper classes for each primitive that supports conversion and formatting of numeric values.
| Data Type | Wrapper Class |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
boolean | Boolean |
char | Character |
Examples of wrapper classes
String stringVariable = "123.4"; Double doubleVariable = Double.parseDouble(stringVariable); var byteValue = doubleVariable.byteValue(); var intValue = doubleVariable.intValue(); var floatValue = doubleVariable.floatValue(); var stringValue = doubleVariable.toString();
While all primitive data types are signed, meaning that both negative and positive values are supported, the Long and Integer methods support unsigned operations if required.
var unsigned = Integer.parseUnsignedInt("3000000000");
System.out.println("Unsigned value is " + unsigned);
var result = Integer.divideUnsigned(unsigned, 2);
System.out.println("The result is " + result);
