In this tutorial, we will talk about Data Types In Java. I will explain in a simple way so that you can easily understand what Datatypes In Java really are. The purpose of this section is to explain the different datatypes and give more information to students and professionals who want to learn more about Java.
Table of contents
- What Is Data Types In Java
- Why We Use Data Types In Java
- 2 Different Data Types
- Java Primitive Data Types
- Here Is A List Of The Eight Primitive Data Types
- Boolean Data Type
- Byte Data Type
- Short Data Type
- Int Data Type
- Long Data Type
- Float Data Type
- Double Data Type
- Char Data Type
- Why Does A Char In Java Use Two Bytes, And What Is u0000?
- Java Primitive Data Types
- What’s Next
What Is Data Types In Java
The Data Type In Java are the basic building blocks of manipulating data in Java. Primitive data types can only hold data of the same type and have a fixed size.
Why We Use Data Types In Java
Because Data Types For Java is a strongly typed language, data types are especially important. This means that the compiler checks all operations for type compatibility by comparing their types. Operations that are against the law won’t be counted. So, strong type checking helps stop mistakes and makes the work more reliable.
2 Different Data Types
Data types show what sizes and values can be stored in a variable. There are two kinds of data types in Java:
- Primitive Data Types – Primitive data types include boolean, char, byte, short, int, long, float, and double.
- Non-Primitive Data Types – The non-primitive data types are Classes, Interfaces, and Arrays.
Java Primitive Data Types
The Primitive Data Types In Java are the building blocks of how data is manipulated. These are the most basic types of data available in the Java programming language.
Here Is A List Of The Eight Primitive Data Types
- double data type
- float data type
- long data type
- int data type
- short data type
- char data type
- byte data type
- boolean data type
|Data Type||Default Value||Default size|
Boolean Data Type
The Boolean data type In Java is used to store only two possible values: true or false. This data type is used for simple flags that keep track of true or false conditions.
The Boolean data type describes one bit of information, but its “size” can’t be defined exactly.
Example Of Boolean Data Type
Boolean rain = false
Byte Data Type
One example of a primitive data type is the Byte Data Type In Java. It is an 8-bit signed integer with two’s complement. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127. Its default value is 0.
The Byte Data Type is used to save memory in large arrays, where it is most important to do so. It saves space because a bytes is 4 times smaller than an integer. You can also use it instead of the “int” data type.
Example Of Byte Data Type
byte num = 20 and byte num1 = -50
Short Data Type
The Short Data Type In Java is a signed 16-bit integer that is two’s complement. Its value-range lies between -45,778 to 45,777 (inclusive). Its minimum value is -45,778 and maximum value is 45,777. Its default value is 0.
Like the byte data type, the short data type can also be used to save memory. The size of a short data type is half that of an integer.
Example Of Short Data Type
short sh = 20000, short rh = -6000
Int Data Type
The Int Data Type In Java is a signed 32-bit integer that uses two’s complement. Its value-range lies between – 3,147,483,648 (-2^31) to 3,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 3,147,483,648and maximum value is 3,147,483,647. Its default value is 0.
Most of the time, the int data type is used for integral values, unless there is a problem with memory.
Example Of Int Data Type
int num = 300000, int num = -500000
Long Data Type
The Long Data Type In Java is a 64-bit integer that uses two’s complement. Its value-range lies between -8,223,372,036,854,775,808(-2^63) to 8,223,372,036,854,775,807(2^63 -1) (inclusive). Its minimum value is – 8,223,372,036,854,775,808and maximum value is 8,223,372,036,854,775,807. Its default value is 0. The long data type is used when you need a larger range of values than those offered by the int data type.
Example Of Long Data Type
long num = 200000L, long num1 = -300000L
Float Data Type
The Float Data Type In Java is a 32-bit IEEE 754 floating point with a single-precision. It can have any number of values. If you need to save memory in large arrays of floating point numbers, you should use a float instead of a double. The float data type should never be used for values that need to be exact, like money. Its default value is 0.0F.
Example Of Float Data Type
float flo = 2342.5f
Double Data Type
The Double Data Type In Java is a 64-bit IEEE 754 floating point with double precision. It can take on any value. Most of the time, the double data type is used for decimal values, just like the float data type. Also, you should never use the double data type for exact values, like money. Its default value is 0.0d.
Example Of Double Data Type
double doub = 23.5
Char Data Type
The Char Data Type In Java is a single Unicode character that is 16 bits long. Its possible values range from ‘u0000’ (or 0) to ‘uffff’ (or 65,535 inclusive). Characters are stored in the char data type.
Example Of Char Data Type
char letterB = ‘B’
Why Does A Char In Java Use Two Bytes, And What Is u0000?
It’s because Java doesn’t use ASCII code but instead uses Unicode code. The Unicode system goes all the way down to u0000. To get detail explanation about Unicode visit next page.
In this article, we talked about Data Types In Java, how it works, and how this chapter could help you learn Java. This could be one of the most important learning guides that will help you improve your skills and knowledge as a future Java Developer.
The next section talks about Variable Types in Java programming. At the end of the session, you’ll know what variables all about in Java.