Data Types in Java

Data Types in JAVA

In this article, I am going to discuss the Data Types in Java with examples. Please read our previous article, where we discussed how to write, compile, and execute a Java Program in detail. As a developer, it is very important for you to understand the Data Type in Java. This is because you need to decide which data type to use for a specific type of value. As part of this article, we are going to discuss the following pointers in detail.

  1. Why we need data types in Java?
  2. What are the data types in Java?
  3. Classification of Java Data Types
  4. Primitive & Non-Primitive Data Types
Why we need data types in Java?

The Data Types in Java is basically used to store the data temporarily in the computer through a program. In the real world, we have different types of data like integer, floating-point, character, string, etc. To store all these different types of data in a program to perform business-related operations, we need the data types.

What is a data type in Java?

The Data types are something which gives information about

  1. Size of the memory location.
  2. The range of data that can be stored inside that memory location
  3. Possible legal operations that can be performed on that memory location.
  4. What types of results come out from an expression when these types are used inside that expression.

The keyword which gives all the above information is called the data type.

Classification of Java Data Types:

The Data Types in Java specifies the size and type of values that can be stored in an identifier. The Java language is rich in its data types. Data types in Java are classified into two types:

  1. Primitive Types: Examples: Integer, Character, Boolean, and Floating Point.
  2. Non-primitive Types: Examples: Classes, Interfaces, and Arrays.

For a better understanding of Java Data Types, please have a look at the following image which gives you an overview of different kinds of data types.

Data Types in Java

Note: Java is a strictly or strongly typed programming language i.e. before using the variable, first you need to declare it using data type (either primitive or non-primitive data type). 

Non Primitive Data Types:

non-primitive data type is something else such as an array structure or class is known as the non-primitive data type.

Primitive Data Types:

Primitive data types are only single values, they have not special capabilities. Java defines eight primitive types of data: byte, short, int, long, char, float, double, and boolean. The primitive data types are also commonly referred to as simple types.

The primitive data types can be put in four groups:
  1. Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers.
  2. Floating-point numbers: This group includes float and double, which represent numbers with fractional precision.
  3. Characters: This group includes char, which represents symbols in a character set, like letters and numbers.
  4. Boolean: This group includes boolean, which is a special type for representing true/false value.
Integers :

Java defines four integer types: byte, short, int, and long. All of these are can hold signed, positive and negative values.

Byte Data Type:

The smallest integer type is a byte. Variables of type are byte is especially used when you’re working with raw binary data that may not be directly compatible with java’s other built-in types. It is an 8-bit signed two’s complement integer and can hold a value between -128 to 127 (inclusive). That means its minimum value is -128 and the maximum value is 127 whereas its default value is 0.

As it is the smallest integer type (4 times smaller than int data type), so you can use this byte data type instead of int data type when the value is between -128 and 127. This is because is saved memory in large arrays where the memory savings is most required. 

Examples: byte b1 = 100; byte b2 = -100;

Short Data Type:

The short Data Type in Java can hold a value between -32,768 to 32,767 (inclusive). That means its minimum value is -32,768 and the maximum value is 32,767 whereas its default value is 0. It is a 16-bit signed two’s complement integer. 

Like the Byte data type, the short can also be used to save memory as it is 2 times small than the int data type.

Examples: short s1 = 5000; short s2 = -777;

Int Data Type:

The most commonly used integer type is int. In addition to other uses, variables of type int are commonly employed to control loops and to index arrays. It is a 32-bit signed two’s complement integer and this data type can hold a value between  -2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). That means its minimum value is -2,147,483,648 and maximum value is 2,147,483,647 whereas its default value is 0

The int data type is generally used as a default data type for integral values unless if there is no problem with memory.

Examples: int i1 = 50000; int i2 = -50000;

Long Data Type:

It is useful for those occasions where an int type is not large enough to hold the desired value. It is a 64-bit two’s complement integer and this data type can hold a value between  -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). That means its minimum value is -9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807 where as its default value is 0.

Examples: long l1 = 666000L; long l2 = -222000L;

Floating-point Types:

Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. Java Provides two data types to hold floating-point values. They are as follows.

  1. Float Data Type
  2. Double Data type
Float Data type:

It specifies a single-precision 32-bit IEEE 754 floating-point, which is faster on some processors and takes half as much space as double-precision. So, it is recommended to use float instead of double data type if you want to save memory in large arrays of the floating-point numbers. Its value range is unlimited and its default value is 0.0F. This data type should not be used for precise values, such as currency.

Examples: float f1 = 123.4f; float f2 = 768.5f;

Double Data Type:

It specifies a single-precision 64-bit IEEE 754 floating-point, which is actually faster than single-precision on some modern processors that have been optimized for high-speed mathematical calculations. Its value range is also unlimited like a float data type. Its default value is 0.0d.

Example: double d1 = 345.6; double d2 = 125.7; 

Char Data Type:

In Java, the data types used to store characters are char. Java uses Unicode to represent characters. There are no negative chars. Unicode defines a fully international character set that can represent all of the characters found in all human languages like, Latin, Greek, Arabic, and many more. This data type is a single 16-bit Unicode character and its value-ranges between ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive). 

Example: char ch = ‘A’;

Why char uses 2 bytes in java and what is \u0000?

This is because java uses the Unicode system instead of the ASCII code system and the \u0000 is the lowest range in the Unicode system. In our upcoming articles, we will discuss more the Unicode System.

Boolean Data Type:

Java has a primitive type called boolean, which is used to store two possible values i.e. true or false. The default value is false. This data type in Java is basically used for simple flags that track true/false conditions.

Example: Boolean b1= true;

The values, ranges, and sizes of these primitive data types are shown in this table:

Data Types Valid Values Default Values Size Range Example
boolean true, false false 1 bit boolean isEnabled = true;
byte integer 0 8 bits byte thiss =1;
char unicode \u0000 16 bits char a =’a’;
short integer 0 16 bits [-32,768, 32,767] short counter =1;
int integer 0 32 bits [-2,147,483,648, 2,147,483,647] int I = 10;
long integer 0 64 bits [-9,223,372,036,854,775,808, 9,223,372,036,854,775,807] long song = 100;
float floating point 0.0 32 bits float pi = 3.14F;
double floating point 0.0 64 bits Double team = 1e1d;

These are the data types in Java. The primitive data types are much like the C++ data types. But, unlike C++, String is not a primitive data type. Strings in Java are objects. The Java library has the String class and we create their objects when we deal with strings. When it comes to numeric data types, Java does not have any notion of unsigned integers. So, the numeric data types in Java can always store positive and negative values. We will talk about the non-primitive data types later. 

In the next article, I am going to discuss Literals in Java with examples. Here, in this article, I try to explain Data Types in Java with some examples. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *