Data Types in Java

In this post, I am going to discuss Data Types in Java programming language.
Humans know the difference between “54321” and “zyxwv”. But a machine does not know it. We use Data Types in computer programming to dictate what a variable or object can hold. We use Data types in all programming languages including C, C++, Java.

In the previous post, I have told you that you have to declare the Data Type while declaring variables.

Example :
int mySalary = 50000; // number (whole number)
float mySalary = 50000.00f; // number (floating point number)
char firstLetter = 'A'; // character
boolean isOnline = true; // boolean
String channelName = "Programmer Kansabanik"; // String

We can divide Data Types into 2 groups. They are :

  • Primitive data types
  • Reference data types

Primitive Data Types :

Primitive types are the most basic data types available within the Java language. We can say that a primitive variable’s information is stored as the value of that variable.

A primitive data type specifies the size and type of variable values. We can classify them into 8 types. They are: 

  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. boolean
  8. char
Data TypeSizeDescription
byte1 byteStore whole numbers
short2 bytesStore whole numbers
int4 bytesStore whole numbers
long8 bytesStore whole numbers
float4 bytesStore fractional numbers
double8 bytesStore fractional numbers
boolean1 bitStore true or false values
char2 bytesStore a single character/letter or ASCII values
This data is taken from w3school.com

Primitive Numbers :

We can divide primitive numbers into two types. They are:

  1. Integer types (stores whole numbers, positive or negative, without decimals. They are byte, short, int and long. You have to choose the correct data type based on the numeric value.).
  2. Floating point types (stores decimal or fractional numbers. They are : float and double.).

Note : int (for integer numbers) and double (for floating numbers) is the most used number variables in Java programming language.

Integer Types
Byte

All numbers in the interval (-128, 127) can be stored in a byte variable in Java programming language. The interval between -128 to 127 is smaller than the interval of an int variable. So we can say that the difference between a byte and int variable is the range of value.

Programmers use byte data type to save space in large arrays in Java programs. They take less space than int variables.

  • -2^7 = -128
  • 2^7 -1 = 127
Example :
byte exampleOne = -128; //Ok
byte exampleTwo = 127; //Ok
byte exampleThree = 100; //Ok

byte exampleFour = -129; //error
byte exampleFive = 128; //error

Short

All numbers in the interval (-32768 to 32767) can be stored in a short variable. A short data type is a 16-bit signed two’s complement integer.

A short variable takes space in memory less than int variable but more than a byte variable.

  • -2^15 = -32,768
  • 2^15 -1 = 32767
Example :
short exampleOne = -32768; //Ok
short exampleTwo = 32767; //Ok
short exampleThree = 4000; //Ok

short exampleFive = 32769; //error
short exampleSix = 32768; //error
Int

You can only store the binary value of an integer in it. Int data type is a 32-bit signed two’s complement integer.
The int data type can store whole numbers from – 2,147,483,648 to 2,147,483,647.

  • -2^31 = -2,147,483,648
  • 2^31 -1 = 2,147,483,647

Programmers generally use int as the default data type for integral values unless there is a concern about memory. The value of int is 0 in Java programming language.

Long :

The long data type can store whole numbers from -9223372036854775808 to 9223372036854775807. A long data type is a 64-bit signed two’s complement integer.

programmers use the long data type for very large numbers where int is not sufficient for that value.

Note : Add a “L” to avoid any kind of errors in your program while using a long data type for a very long value.

Example :
long exampleOne = 15000000000L; // Ok
long exampleTwo =  15000000000; // error
long exampleThree = 15000; // Ok
/* 
Because 15000 is considered here as an int value
for the exampleThree variable.
*/

Floating Point Types

programmers use floating-point types whenever they store a number with decimal value or fractional value in Java.

Float :

You need to use float data type to store fractional numbers from 3.4e−038 to 3.4e+038.

Note : You should add an “f” at the end of the value in your Java program. Otherwise, you may face errors.

The float data type is a single-precision 32-bit IEEE 754 floating-point. Programmers use float to save memory in large arrays of floating-point numbers.

Example :
float myHeight = 5.2f;
//We have to add f at the end for decimal values.
System.out.println(myHeight);
Double :

The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Double data type is a double-precision 64-bit IEEE 754 floating point.

Note : We need to put “d” at the end of the value.

This data is taken from “www.w3school.com”.

We use double as the default data type for decimal values in java programs.

Example :
double myDouble = 19.99d;
System.out.println(myDouble);

Boolean :

A Boolean data type can only have a value of either true or false. This data type is only used for logical values.

In Java program to store true and false, we must use the boolean variable as shown below.

Example :
boolean bol1 = true;
boolean bol2 = false;

System.out.println(bol1);
System.out.println(bol2);

The function of the boolean data type is different from other primitive data types in Java. The boolean data type is used in the validations/verifications/checking purposes.

Char :

You can store a single character using a char variable. You need to surround them with single quotes.

char firstLetter = 'A';
System.out.println(firstLetter);

char data type is a single 16-bit Unicode character.

Reference Data Types :

We can say that a reference variable holds a reference to information related to that variable. A reference variable contains the reference ( address ) of dynamically created objects. These kinds of variables (non-primitive) are not predefined like primitive data types and they refer to objects.

A reference type is based on a class. They are used to access objects.

When you will declare a reference type variable, use the class name as the data type of that variable.

Example (Print a String variable):
String myChannel = "Programmer Kansabanik";
System.out.println(myChannel);

Published by Surojit Kansabanik

My name is Surojit Kansabanik. I am a student by profession and a programmer by passion. I like to share my programming knowledge through my YouTube channel (Programmer Kansabanik) and blogs (Programmer Kansabanik) with others. I like to guide newbies in the files of coding.

Leave a comment

Design a site like this with WordPress.com
Get started