Last Updated: 10 July, 2023
In programming languages, a data type is a classification of data that tells the compiler or interpreter of that programming language how the programmer intends to use the data.
Java is a statically typed programming language, which means that variables must be declared with a specific data type before they can be used.
Java supports various types of data like String, Integer, Character, Decimals, Boolean, etc. These different data types allocate some memory and accept values according to their nature.
When choosing a data type for a variable, it is important to consider the size and range of values that the variable will need to store. For example, if the variable will only store whole numbers from 0 to 99, then the byte data type would be a good choice.
There are two types of data types supported by Java:
Primitive data types are predefined by the Java language and named by a keyword. Primitive values do not share a state with other primitive values.
There are a total of 8 primitive data types in Java: byte, char, short, int, long, float, double, and boolean. Every primitive data type has a default value and occupies a different amount of memory.
See below all 8 primitive data types' names and details about each in the given table.
Data Type | Range of Values | Default Value | Allocates Memory |
---|---|---|---|
boolean |
true / false | false | 1 bit |
byte |
-128 to 127 | 0 | 8 bits |
char |
Character representation of ASCII values 0 to 255 | '\u0000' | 2 byte |
short |
-32768 to 32767 | 0 | 16 bits |
int |
- 2,147,483,648 to 2,147,483,647 | 0 | 32 bits |
long |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L | 64 bits |
float |
3.4e−038 to 3.4e+038 | 0.0F | 32 bits |
double |
1.7e−308 to 1.7e+308 | 0.0d | 64 bits |
Example : A sample Java program of Primitive Data-types
Output
byteData : 0
shortData : 0
intData : 0
longData : 0
floatData : 0.0
doubleData : 0.0
charData :
booleanData : false
A non-Primitive data type is also known as a Reference data type in Java. The Reference Data Types don't store variable values directly in memory; they only contain the memory address of those variable values. For example, user-defined classes, String, ArrayList, etc.
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com