Primitive data types are the simplest types in every programming language in general and Java in particular. In Java, we have eight primitive data types:
- char
- byte
- short
- int
- long
- float
- double
- boolean
Variables defined with primitive data types are called primitive variables. In this tutorial, we will look at the primitive data types in Java, how to create and assign initial values to a primitive variable.
Below is a summary table of primitive types and their value ranges:
| Primitive type | Size | Range value |
|---|---|---|
byte |
8-bit | -128 to 127 |
short |
16-bit | -32,768 to 32,767 |
int |
32-bit | -2³¹ to 2³¹−1 |
long |
64-bit | -2⁶³ to 2⁶³−1 |
char |
16-bit | '\u0000' to '\uffff' |
float |
32-bit | IEEE 754 binary32 |
double |
64-bit | IEEE 754 binary64 |
boolean |
— | true / false |
char
The syntax for defining a variable of type char is as follows:
|
1 |
char <variable_name> = 'value of variable'; |
For example:
|
1 |
char a = 'A'; |
As you can see, the value of the char variable will be placed in a single quotation mark, if you put it in double quotation marks like declaring a variable for a String, then a compile error will occur.

A char is an unsigned integer (an integer without a “+” or “-” sign following it, also known as a positive number), 16 bits, representing a UTF-16 code unit of the Unicode character set. Its value is represented in Unicode in the range from “\u0000” (0) to “\uffff” (65535). Because some Unicode code points require 2 code units, a char cannot be used to define every Unicode character!
In Java, the char variable is stored as a positive number from 0 to 65535.
In the example above, we declared the variable ‘a’ with the value ‘A’. This character ‘A’ has a Unicode code point of U+0041, meaning its value is 65, similar to the ASCII (American Standard Code for Information Interchange) code. Therefore, another way to declare a char variable in Java is as follows:
|
1 |
char a = 65; |
When running the above code:
|
1 2 3 4 5 6 7 8 9 |
package com.huongdanjava; public class Example { public static void main(String[] args) { char a = 65; System.out.println(a); } } |
The result will be A.
You can refer to the ASCII code table below:

As I said above the value of the char variable is a positive number, so if you assign it a negative value, it will have a compile error:

But if you declare char variables with negative numbers by pressing the type, it is still ok, no compile error:

But when you run, the result will always be:

Because at this point -65 in 32 bits is cast to 16 bits, it will be code point 65471 (/uFFBF). This code point has not been assigned to any character yet, so Java will print a replacement character, in this case the “?” character.
byte
A byte is an 8-bit data type with values ranging from -128 to 127.
The byte variable declaration example is as follows:
|
1 |
byte a = 83; |
short
short is larger than byte, 16bits, with values greater than bytes from -32.768 to 32.767.
The short variable declaration is as follows:
|
1 |
short a = 12345; |
int
int is 32 bits in size, ranging from -2,147,483,648 to 2,147,483,647.
Declare the int variable as follows:
|
1 |
int a = 100; |
long
Long is a 64-bit data type, its value ranges from -9.223.372.036.854.775.808 to 9.223.372.036.854.775.807.
The variable declaration is as follows:
|
1 |
long a = 234; |
Some notes for byte, short, int, and long variables
– When declaring a instance variable or static variable, if you do not assign a value to it, then the default value is 0.
– The default data type of integers is int. Meaning when you declare the following:
|
1 |
long a = 1000; |
the number 1000 is int. But the mechanism of automatic conversion from int to long type of Java allows us to declare such. To specify the number 1000 is the long type, you can add the letter L or l after this number and declare as follows:
|
1 |
long a = 1000L; |
– Java allows automatic conversion of variables between primitive types as follows (with both float and double):
- byte to short, int, long, float, or double.
- short to int, long, float, or double.
- char to int, long, float, or double.
- int to long, float, or double.
- long to float or double.
- float to double.
In some cases, this conversion does not guarantee that the converted value will remain the same, for example, in this case:
|
1 2 3 4 |
int i = 16_777_217; float f = i; System.out.println((int) f); |
When you run the example, you’ll see that the printed value is only 16_777_216.
– When you perform calculations with byte or short integers, Java will first automatically convert these variables to int type before performing the calculation. For example, the following code will result in a compile error:

– Java also allows us to represent these variables in binary, decimal, octal and hexadecimal.
- Binary variables are declared starting with 0b or 0B.
For example:
|
1 |
int b = 0b100001011; |
- Decimal is the number we use every day; it is represented as normal.
For example;
|
1 |
int b = 129; |
- The octal variable is declared starting with 0.
For example:
|
1 |
int b = 0235; |
- The hexadecimal variable is declared starting with 0x or 0X.
For example:
|
1 |
int b = 0xA; |
– Since Java 7, when declaring variables with large values, we can use underscores between numbers in our favor so that numbers can be more readable.
For example:

You can use underscores to declare variables in Java 7 with the following guidelines:
- You can not declare the underscore at the beginning or at the end of a number.
- You can not put the underscore immediately after 0b, 0B, 0x, 0X in the binary number and number in the 16th system.
- You can put the underscore after the zero in the declaration number in the 8th system.
- You can not put underscores before the letter L or other similar words (the letter L marks the value of the variable in the long type).
This tutorial, I introduced only the first five data types, the next part I will introduce you about float, double and boolean.
