Data Types in Java: A Beginner’s Guide

Rohan Patel
2 min readJan 2, 2023

--

Data types are an essential concept in any programming language, and Java is no exception. In Java, a data type determines the kind of values a variable can store and the operations that can be performed on it. There are two main categories of data types in Java: primitive types and reference types. Primitive types are the most basic data types and include types such as int, double, and char. Reference types, on the other hand, are used to refer to objects, which are created from class definitions and can contain both data and behavior (methods). In this article, we’ll take a closer look at the different data types in Java and how to use them effectively in your code.

In Java, there are eight primitive data types:

  1. byte: 8-bit signed integer (-128 to 127)
  2. short: 16-bit signed integer (-32768 to 32767)
  3. int: 32-bit signed integer (-2147483648 to 2147483647)
  4. long: 64-bit signed integer (-9223372036854775808 to 9223372036854775807)
  5. float: 32-bit single-precision floating point
  6. double: 64-bit double-precision floating point
  7. char: 16-bit Unicode character (0 to 65535)
  8. boolean: true or false

Here are a few examples of how to use the primitive data types in Java:

byte: 8-bit signed integer (-128 to 127). In this example, the variable “b” is declared as a byte and initialized with the value 100.

byte b = 100;

short: 16-bit signed integer (-32768 to 32767). In this example, the variable “s” is declared as a short and initialized with the value 10000.

short s = 10000;

int: 32-bit signed integer (-2147483648 to 2147483647). In this example, the variable “i” is declared as an int and initialized with the value 1000000.

int i = 1000000;

long: 64-bit signed integer (-9223372036854775808 to 9223372036854775807). In this example, the variable “l” is declared as a long and initialized with the value 1000000000.

long l = 1000000000;

float: 32-bit single-precision floating point. In this example, the variable “f” is declared as a float and initialized with the value 3.14. Notice that we use the “f” suffix to indicate that the value is a float.

float f = 3.14f;

double: 64-bit double-precision floating point. In this example, the variable “d” is declared as a double and initialized with the value 3.14159265358979323846.

double d = 3.14159265358979323846;

char: 16-bit Unicode character (0 to 65535). In this example, the variable “c” is declared as a char and initialized with the character ‘A’.

char c = 'A';

boolean: true or false. In this example, the variable “flag” is declared as a boolean and initialized with the value true.

boolean flag = true;

Overall, these examples demonstrate how to use the different primitive data types in Java to store a variety of different kinds of data.

--

--