Data types

Learn what the different data types are and how they are used.

Java Types

Basic Data Types

In Java before we make a variable, we need to declare what type it is. A type decides what can be stored in the variable such as numbers only or all the letters in the alphabet.

We declare it as such:

int beatsPerMinute = 43;

As of now, Java has suppport for eight primitive data types. They are called primitive because they are predefined within the language and named by their respective keyword.

byte type

A byte type is an 8-bit signed integer with a minimum value of -128 and maximum value of 127. It is also space saving when used in large arrays in place of an int type (4x smaller).

byte a = 109, byte b = -123

short type

A short type is a 16-bit signed integer with a minimum value of -32,768 and a maximum value of 32,767 (2x smaller than an int).

short a = 12,354, short b = -32,764

int type

An int type is a 32-bit signed integer with a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647.

int a = 1,200,238 int b = -1,317,234,423

long type

A long type is a 64-bit signed integer with a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807.

This is twice as big as the int type, it is used when the range needed is more than the supported range of the int type.

long a = 10,001,023L, long b = -1,242,835,234L

float type

This data type is a 32-bit IEEE 754 floating point value. It is used for decimal values like the double data type, however it saves space as it only takes half the space of the double type.

float exFloa = 342.7f

double type

This data type is a 64-bit IEEE 754 floating point value. It is used for decimal values and is the default type to use when space is not concerned.

double exDoub = 174.6

boolean type

Boolean data types have only one of two values: true or false.

Generally flagged for true/false conditions.

boolean a = true, boolean b = false

char type

The char type can be used to store any character.

Characters can include letters, numbers and symbols.

char firstLetter = 'A'

Literal Data Types

Literal data types are a use of the primitive data type with a fixed value.

byte b = 68;
short s = 6575;
int i = 324530;
long lng = 1,034,553;
float flot = 345.9f;
double dubl = 165.8;
char aChr= 's';

Reference Data Types

These are reference variables that have defined constructors of the class.

The constructor is used to access objects.

Array arr = new Array();

Comments

blog comments powered by Disqus