Data types in java

Snippets
2 min readDec 13, 2023

--

Data Types in Java

Data types are fundamental building blocks for storing and manipulating data. They define the type of values that variables can hold and the operations that can be performed on those values. Java offers two main categories of data types: primitive data types and reference data types.

Primitive Data Types

Primitive data types are basic data types that represent fundamental values, such as numbers, characters, and boolean values. They are directly stored in the memory and are not associated with any objects. There are eight primitive data types in Java:byte: A byte represents an 8-bit signed integer, ranging from -128 to 127.

byte age = 30;
short temperature = 25;
int population = 1000000;
long distance = 1234567890L;
float pi = 3.14159265358979323846F;
double g = 9.80665;
boolean isSunny = true;
char firstLetter = ‘A’;

short: A short represents a 16-bit signed integer, ranging from -32,768 to 32,767.

int: An int represents a 32-bit signed integer, ranging from -2,147,483,648 to 2,147,483,647.

long: A long represents a 64-bit signed integer, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

float: A float represents a 32-bit single-precision floating-point number, capable of storing approximately 7 decimal digits of precision.

double: A double represents a 64-bit double-precision floating-point number, capable of storing approximately 15 decimal digits of precision.

boolean: A boolean represents a logical value, either true or false.

char: A char represents a 16-bit Unicode character.

Reference Data Types

Reference data types, also known as object types, represent instances of classes. They store references to objects in memory, allowing you to work with objects and their associated properties and methods. Reference data types include arrays, strings, and objects of user-defined classes.

ex

int ar1[]= new int[5];

--

--