Everything about primitive types in Java

Jannayna Araujo
2 min readOct 31, 2023

--

If you’re learning Java now, have 1 year of experience, or even 20, at some point in your career, you’ve probably wondered which is the best type for your variable. And, let’s be honest, this is a topic we learn back in the early days of our studies and may not necessarily remember forever.

With that in mind, in this article, I aim to provide a concise and objective overview of the specifics of each primitive type in the Java language, including their sizes, accepted value ranges, and default values.

To better understand primitive types, we first need to understand what a bit is. According to Wikipedia,

A bit is the smallest unit of information that can be stored or transmitted, used in Computing and Information Theory. A bit can only assume 2 values: 0 or 1, representing off or on, respectively.

In this way, we consider that each 1 bit can store 2 possibilities/values. If I have a storage capacity of 8 bits, it means:

2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 256 possibilities/values

The number of bits represents the number of possible values that a variable can store.

With this information in hand, we can now speak more confidently about primitive types in Java. In total, we have 8 primitive types, divided into 4 groups:

  1. Integer numeric types: byte, short, int, and long.
  2. Floating-point numeric types: float and double.
  3. Unicode characters: char.
  4. Boolean value: boolean.

Integer numeric types

Store integer values (both negative and positive). Examples: -5, 245, 0.

byte

  • Size: 8 bits
  • Possible values: from -128 to 127 (remember the calculation we did above, and note that both negative and positive values are considered here)
  • Default value: 0 (zero)

short

  • Size: 16 bits
  • Possible values: from -32768 to32767
  • Default value: 0

int

  • Size: 32 bits
  • Possible values: from -2174483648 to 2147483647
  • Default value: 0

long

  • Size: 64 bits
  • Possible values: from -9223372036854770000 to 9223372036854770000
  • Default value: 0L

Floating-point numeric types

Store integer values (both negative and positive) with decimal places. Example: -5.8.

float

  • Size: 32 bits
  • Possible values: from -1,4024E-37 to 3,4028E+38
  • Default value: 0,0f

double

  • Size: 64 bits
  • Possible values: from -4,94E-307 to 1,79E+308
  • Default value: 0,0

Unicode characters

Stores only 1 character (for more information about character codes, visit https://symbl.cc/en/unicode/table/).

char

  • Size: 16 bits
  • Possible values: from ‘\u0000’ to ‘\uFFFF’
  • Default value: ‘\u0000’

Boolean value

Stores true or false values.

boolean

  • Size: 1 bit
  • Possible values: true or false
  • Default value: false

Knowing the primitive types in Java and how much information each of them can store makes it much easier to decide which is the best type for your variable, optimizing your application’s memory usage.

--

--

Jannayna Araujo
Jannayna Araujo

Written by Jannayna Araujo

Hi :) If you're here, you can call me Janna ❤ ​ I'm a Kotlin and Java Senior backend developer, Tech Leader, passionate about technology.