Table of Contents
Classifying data into different data types (or more often, simply ‘types’) is a fundamental preoccupation of programming. Most programming languages categorize data into strict categories. This is particularly true for strongly typed languages (i.e. languages which do not permit data from one type to be used in another type) like Java.
You can learn more about data types in Java in this article.
In this blog , we will learn about the 8 data types in Java and their characteristics.
Data Types in Java
Java is a strongly typed language. Although there are no universally agreed definitions of what ‘strongly typed’ actually means, general meaning holds that a ‘strongly typed’ language does not permit data from one declared variable type to be used in another. The implication, is that you have to explicitly declare what type of data a variable belongs to before declaring it.
You undertake such “strongly typed” declarations in real life all the time. When you say, “John is a man”, you essentially declare that: a) John is male, and b) John is old enough to not be a boy or child. Henceforth, for all practical purposes, John will remain classified as a ‘man’ and cannot be used in other similar categories (like ‘woman’, ‘boy’, etc.).
Grab the opportunity to learn Java with Entri! Click Here
It’s the same case with data types in languages like Java. Once you say that a variable is an integer, it cannot be used to store decimal values or strings.
Data types are divided into two groups:
- Primitive data types – includes
byte
,short
,int
,long
,float
,double
,boolean
andchar
- Non-primitive data types – such as Strings, Arrays and Classes
Primitive Data Types
1: What is the default value of a boolean in Java?
A primitive data type specifies the size and type of variable values, and it has no additional methods.
There are eight primitive data types in Java:
Data Type | Size | Description |
---|---|---|
byte |
1 byte | Stores whole numbers from -128 to 127 |
short |
2 bytes | Stores whole numbers from -32,768 to 32,767 |
int |
4 bytes | Stores whole numbers from -2,147,483,648 to 2,147,483,647 |
long |
8 bytes | Stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float |
4 bytes | Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits |
double |
8 bytes | Stores fractional numbers. Sufficient for storing 15 decimal digits |
boolean |
1 bit | Stores true or false values |
char |
2 bytes | Stores a single character/letter or ASCII values |
Grab the opportunity to learn Java with Entri! Click Here
Free Tutorials To Learn
SQL Tutorial for Beginners PDF – Learn SQL Basics | |
HTML Exercises to Practice | HTML Tutorial | |
DSA Practice Series | DSA Tutorials | |
Java Programming Notes PDF 2023 |
Java Primitive Data Types
Any data type built-into a programming language is called ‘primitive’ data type (the word itself betrays its meaning). Built-in data types are the basic building blocks of a programming language. It is often possible to combine them to create composite data types. The basic behavior of a primitive data type itself, however, cannot be modified (it is, after all, something primitive to the language – like the human Id).
Ace your Java skills with Entri! Hurry up !
There are eight primitive data types in Java. These are as follows:
1. Byte: A byte, for those of you who skipped CS 101, is one of the most basic units of memory made up of 8 individual bits. Byte data types in Java have the following characteristics:
-
Minimum Value: -128 (2^7)
-
Maximum Value: 127 (2^7-1)
-
Default Value: 0
Examples:
byte x = 56
byte y = 68
Thus, you can save numbers between -128 and 127 (inclusive) in a byte. Bytes, because of their size, are useful for storing small data in large arrays. Another programmer looking through your code will also instantly recognize that a byte type will hold only a small value, thus improving your code’s readability (a major issue for large applications).
Grab the opportunity to learn Java with Entri! Click Here
2. Short: A short is twice the size of a byte, i.e. it is made up of 16-bits. Its chief characteristics are:
-
Minimum Value: -32,768 (2^15)
-
Maximum Value: 32,767 (2^15-1)
-
Default Value: 0
Examples:
short x = 9870
short y = -635
Like bytes, short types are useful alternatives to int (see below) data types, particularly if your data falls within the specified range. As with byte, using short also improves code readability, besides saving memory.
Grab the opportunity to learn Java with Entri! Click Here
3. Int: An integer is four times the size of a byte (i.e. it is made up of 32 bits). It is one of the most commonly used data types in Java.
-
Minimum Value: -2,147,483,648 (2^31)
-
Maximum Value: 2,147,483,647 (2^31 – 1)
-
Default Value: 0
Examples:
int x = 150000
int y = -2004320
As the most easily understood data type, you will use int a lot in your code.
Ace your Java skills with Entri! Hurry up !
4. Long: A long data type is twice the size of an integer, i.e. it is made up of 64-bits. It’s chief characteristics are:
-
Minimum Value: -9,223,372,036,854,775,808 (2^63)
-
Maximum Value: 9,223,372,036,854,775,807 (2^63 – 1)
-
Default Value: 0
Examples:
long x = 6778005876543
long y = -554233254242
You’ll use long only if you encounter data that doesn’t fit within the int range (which will be rare).
Grab the opportunity to learn Java with Entri! Click Here
5. Float: In programming, any decimal or fractional value is called a ‘float’. If there is a decimal after the number, it will be classified as a float. In Java, a float is made up of 32-bits IEEE floating points*.
The minimum/maximum value of float is not the same as that of the int data type (despite both being made of 32-bits). The full range of float values is beyond the scope of this tutorial. For now, the only thing you need to know is that you’ll use float (and double – see below) for saving decimal values.
Examples:
float x = 2.321
float y = 1.234
*The float value range depends on the IEEE standard classification for floating point numbers.
Ace your Java skills with Entri! Hurry up !
6. Double: Double is a data type that is twice the size of a float. I.e. it is made up of 64-bit IEEE floating points.
As with float, discussing the minimum/maximum value of double data type is beyond the scope of this article. What you should know is that double is a much more precise type than float. For all practical purposes, it is recommended that you use double instead of float for storing decimal values.
Ace your Java skills with Entri! Hurry up !
Examples:
double a = 1.245240
double y = 12.2232
7. Char: Char data type refers to a single 16-bit Unicode character. Unicode is a computer industry standard for representing text related data. This includes alphabets, symbols ($, &, *, #, @, !, etc.), and special figures such as ¢, £, ¥, etc. The Unicode character set includes over 110,000 characters covering more than 100 language scripts.
In other words, any data besides numbers goes into the char data type.
Examples:
char name = ‘John’
char country = ‘USA’
Grab the opportunity to learn Java with Entri! Click Here
8. Boolean: Boolean is the smallest data type in Java, i.e. it is made up of only one bit. Thus, a Boolean data type can have only two values – 0 (or False) and 1 (or True).
Example:
boolean x = true
boolean y = false
(Tip: ‘True’ and ‘False’ written above are not strings. Do not enclose them within quotes as we did with the char example above)
If you found these data types a little confusing, taking a comprehensive Java foundation course would be of great help.
Grab the opportunity to learn Java with Entri! Click Here