Data Types in Java
Data types specify the different sizes and values which can be stored in the variable.
There are two types of data types available in Java Programming Language:
1. Primitive Data Types
2. Non Primitive Data Types
The primitive data types consists boolean, char, byte, short, int, long, float and double.
- 2. Non-primitive data types:
The non-primitive data types consists Classes, Interfaces, and Arrays.
Java Primitive Data Types
In Java Programming language, primitive data types are the building blocks of data manipulation. These are the very basic data types available in Java programming language.
There will be 8 types of primitive data types are available in Java Programming language
All the primitive data types are explained below:
The Boolean data type is the data type which used to store only two possible values: first true and second false. Boolean data type is used for simple flags which track true/false conditions.
The Boolean data type consider one bit of information, but its size can not be defined precisely.
The byte data type is one of the example of primitive data type. Which is an 8-bit signed 2's complement integer.
Byte data type value-range varied between -128 to 127 (inclusive). The minimum value of byte data type is -128 and the maximum value of byte data type is 127. That's default value is 0.
The byte data type is used for save memory in large arrays where the savings of memory is must required. This data type saves space because a byte is 4 times smaller than an integer. Byte data type can also be used in place of "int" data type.
The short data type is a 16-bit signed 2's complement integer. The value range of short data type is lies between -32,768 to 32,767 (inclusive).
The minimum value of short data type is -32,768 and maximum value of short data type is 32,767. The Default value of short data type must be 0.
We can also use the short data type to save memory as like byte data type. The short data type is 2 times smaller than an integer data type.
The int data type is primitive data type and also a 32-bit signed 2's complement integer. Its value-range varies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive).
The minimum value of int data type is - 2,147,483,648 and maximum value of int data type is 2,147,483,647. In int data type"0" is used as a default value.
We should used the int data as a default data type for integral values unless if there is no problem about memory.
The long data type is a 64-bit 2's complement integer. The value range of long data type is varies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive).
The minimum value of the long data type is - 9,223,372,036,854,775,808 and the maximum value of long data type is 9,223,372,036,854,775,807.
Default value of the long data type is 0. The long data type is used when we need such a range of values more than those provided by int.
The float data type is a data type which is also known as a single precision 32-bit IEEE 754 floating point. The value range of float data type is unlimited.
This data type is allowed to use a float (instead of double) if we need to save memory in large arrays of floating point numbers. The float data type is never be used for precise values, as like currency. The Default value of float data type is 0.0F.
The double data type is the data type which is also called a double precision 64-bit IEEE 754 floating point. The value range of double data type is unlimited.
The double data type is mostly used for decimal values as like float data type. The double data type does not used for precise values, as like currency. In double data type "0.0d" is the default value.
The char data type is the data type which is known as a single 16-bit Unicode character.
The value range of char data type is varies between '\u0000' (or 0) to '\uffff' (or 65,535 inclusive). It also used to store characters.
Java Non Primitive Data Types
Non-primitive data types are known as the data type which is created by programmers. These data types are not predefined in Java as like primitive data types.
If we define a variable of non-primitive data types, then it references a memory location where data is stored in the heap memory. Such that it references to a memory where an object is actually placed.
So, the variable of a non-primitive data type is also known as reference data types also called a object reference variable. This reference data types available in the stack memory and the object which it points always lives on the heap memory. The stack stay holds a pointer to the object on the heap.
In Java programming language, all non primitive data types are known as objects which are created by instantiating a class.
There will be mainly 5 types of Non Primitive Data Types are available:
The description of all the five non primitive data types are explained below:
Every class is data type and it is also considered as user defined data types. This can be possible because of a user creates a class.
A string which represents the sequence of characters like America, DEF456, etc. A simplest way of to create a string object which is possible by storing sequence of characters into string type variable as like this:
String str="Apple";
Here, A string type variable str contains "Apple". A string is also called a class.
An array in java is one of the object which is used to store same type multiple variable. That variables can be primitive or non-primitive data type.
To declaring an array variable of primitive data type int is shown below:
int [ ] runs;
To declaring an array variable of non primitive data type is shown below:
Animals [ ] animals; // animals is a name of class.
The interface is declared as like a class but one of the difference is that interface contains only final variables and method declarations. Interface is the fully abstract class.