DEV Community

Cover image for Understanding the Data Types in Java: Part 1
Cal Afun
Cal Afun

Posted on

Understanding the Data Types in Java: Part 1

Introduction

In today's blog, we are going to understand what data types are, and we will continue to dive deep to the first set of data types for whole numbers.

Data types define the kind of value a variable holds and, hence, the type of operations that can be performed on them. For example, a variable of type int can store whole numbers, while a variable of type double can store numbers with decimals. They help the compiler know the amount of memory to allocate to that data type and how to interpret that data type.

Unlike Python, Java is a strongly typed language, which means that every variable must be declared with a specific type before it can be used. This prevents errors like assigning a number to a variable meant for text. For example, you cannot store "Hello" in an int variable. Strong typing ensures code safety, clarity, and easier debugging because the compiler enforces type rules.

In Java, we have 8 types of data types, and today we will discuss four of them.

The Whole Number data types

Four of Java's different data types belong to whole numbers; hence, these data types are all different ways of expressing whole numbers.
They are: int, long, short, byte. What differentiates these types is the fixed sizes and ranges that are consistent, no matter the device you are using. For programming languages like C and C++, the sizes of these data types differ across devices
Below is a table showing the different ranges and memory space each of these data type contain.
Figure 1: Data types and their memory allocation in Java (adapted from Core Java, Volume I, 11th Edition by Cay S. Horstmann, © 2019).

Understanding the Byte data type

byte
byte is the smallest integer type in Java, used to save memory in large arrays or for low-level file and network operations.
Range and storage size:
1 byte (8 bits), range: -128 to 127.
Typical use cases:

  • Reading and writing binary data

  • Memory-sensitive applications

  • Small counters or flags

Understanding the Short data type

short
Short is slightly larger than byte and is used when memory is limited, but a byte’s range is too small.
Range and storage size:
2 bytes (16 bits), range: -32,768 to 32,767.
Typical use cases:

  • Large arrays of numbers where int would waste memory

  • Embedded systems

Understanding the int data type

int
int is the standard integer type for most computations in Java. It’s the default choice for whole numbers.
Range and storage size:
4 bytes (32 bits), range: -2,147,483,648 to 2,147,483,647.
Why int is most commonly used:

  • Large enough for most calculations

  • Faster and more natural for modern processors

  • Default type for integer literals

Understanding the long data type

long
long is for very large whole numbers beyond the range of int.
Range and storage size:
8 bytes (64 bits), range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Typical use cases:

  • Large counters, e.g., tracking population or money in cents

  • Calculations requiring very large numbers

  • Timestamps or IDs

Integer Literals in Java

An integer literal is the way you write whole numbers directly in your code. Java supports several ways to write them, depending on the number system you want to use.

Types Of Integer Literals in Java

  • Decimal Numbers: These refers to the numebers we see and use everyday written in base 10. Eg; 0 , 10, 25, 100...

  • Hexadecimal Numbers: These are numbers written in base 16. These numbers start with 0x or 0X. For example;

int hexNum = 0x1A; // 1A in hex = 26 in decimal
int color = 0xFF00FF; // often used in RGB color codes
Enter fullscreen mode Exit fullscreen mode

For hexadecimal; digits 0–9 and letters A–F, where A=10, F=15.

  • Octal Numbers: These are numbers written in base 8. These numbers start with 0. These are number system is rarely used because of its confusing system. However it is good to know them.They range from 0 to 7.
int octNum = 010; // 10 in octal = 8 in decimal
Enter fullscreen mode Exit fullscreen mode
  • Binary Numbers: These are numbers written in base 2 and range from only 0 to 1. Binary numbers start with 0b or 0B. It useful in low-level programming working with bits. Example;
int binNum = 0b1010; // 1010 in binary = 10 in decimal
Enter fullscreen mode Exit fullscreen mode

Using Underscores in Code For Enhanced Readability
You can put _ between digits to make large numbers easier to read.
These underscores do not tamper with the value of the digit. Just like comments, the compiler ignores underscores in digits. For example;int million = 1_000_000; // same as 1000000
int binary = 0b1111_0000; // same as 0b11110000

Extras you may want to know

If you want to convert from decimal numbers to any other system mentioned above follow the following steps;

  1. Divide the decimal number by the base.
  2. Keep the remainder (this becomes a digit in the new system).
  3. Repeat with the quotient until it’s 0.
  4. Write the digits from last remainder to first (right to left)

Converting 45(decimal number to all types)

  1. Decimal This is the “normal” number system, base 10. Decimal 45 → 45
  2. Hexadecimal (base 16) Symbols: 0–9, A–F Steps: Divide 45 by 16 → quotient 2, remainder 13 Remainder 13 → D in hex Quotient 2 → 2 Decimal 45 → Hexadecimal 0x2D
  3. Octal (base 8) Symbols: 0–7 Steps: Divide 45 by 8 → quotient 5, remainder 5 Quotient 5 → 5 Decimal 45 → Octal 055
  4. Binary (base 2) Symbols: 0,1 Steps: 45 ÷ 2 = 22 rem 1 22 ÷ 2 = 11 rem 0 11 ÷ 2 = 5 rem 1 5 ÷ 2 = 2 rem 1 2 ÷ 2 = 1 rem 0 1 ÷ 2 = 0 rem 1 Write remainders from last to first: 101101 Decimal 45 → Binary 0b101101

Doing the reverse of the above (converting from any data type to decimal)

  1. Binary → Decimal Binary is base 2 (digits 0 or 1). Each position represents a power of 2. Steps: Write the binary number. Example: 101101 Assign powers of 2 from right to left, starting at 0: Position: 5 4 3 2 1 0 Binary: 1 0 1 1 0 1 Multiply each digit by 2^position: 1*2^5 = 32 0*2^4 = 0 1*2^3 = 8 1*2^2 = 4 0*2^1 = 0 1*2^0 = 1 Add them together: 32 + 0 + 8 + 4 + 0 + 1 = 45 Binary 101101 = Decimal 45
  2. Octal → Decimal Octal is base 8 (digits 0–7). Each position represents a power of 8. Steps: Write the octal number. Example: 55 Assign powers of 8 from right to left: Position: 1 0 Octal: 5 5 Multiply each digit by 8^position: 5*8^1 = 40 5*8^0 = 5 Add them: 40 + 5 = 45 Octal 55 = Decimal 45
  3. Hexadecimal → Decimal Hex is base 16 (digits 0–9 and A–F). Each position represents a power of 16. Steps: Write the hex number. Example: 2D (D = 13) Assign powers of 16 from right to left: Position: 1 0 Hex: 2 D Multiply each digit by 16^position: 2*16^1 = 32 13*16^0 = 13 Add them: 32 + 13 = 45 ✅ Hex 2D =** Decimal 45** Summary Multiply each digit by its base raised to its position (counting from 0 at the right). Sum all the products → Decimal value.

Top comments (0)