Data Types In Java | PW Skills 

By | September 19, 2023

Data Types In Java play a fundamental role in programming. It defines the type and range of possible values stored in a Java program. These data types allow us to store different entities efficiently. There are two main categories of data types in Java

Primitive Data Type: Primitive data types represent basic or simple values. They are the building blocks in Java and have a fixed size in memory. There are eight main primitive data types in Java. Some primitive data types are byte, short, int, long, float, char, boolean, etc. 

Secondary Data Type: They are also called Reference data types. It stores references (memory addresses) to complex data structures. These data types in java do not hold the actual data but only the reference data stored. Some of the major secondary data types are objects, arrays, lists, etc. 

Java program development requires a proper understanding of and practical application of these data types. Let us understand various data types in java in detail.

Recommended Course 
  1.  Decode DSA with C++
  2. Full Stack Data Science Pro Course 
  3. Java For Cloud Course 
  4. Full Stack Web Development Course
  5. Data Analytics Course 

Primitive Data Type

The primitive data type is the building block, which consists of basic data types such as bytes, floats, doubles, booleans, etc. There are eight major primitive data types in Java. They are very important for representing and manipulating various types of data in Java. It is essential to be very clear about their characteristics, usage, size, and ranges for proper handling and memory management of your code.

1. Boolean Data Type

This data type stores two possible values, either true or false. It represents a binary state used to make decisions in control structures and logic. It is generally used as a flag that can track true or false conditions in a code.

                                                                                     Boolean Data Type
boolean isJavaFun = true; 

boolean isRaining = false; 

if (isJavaFun) {

    System.out.println (“Java is fun!”);

} else {

    System.out.println (“Java is not fun.”);

}

if (isRaining) {

    System.out.println(“It’s raining outside.”);

else {

    System.out.println (“It’s not raining.”); 

}

 

Output
Java is fun

It is not raining. 

2. Byte Data Type

Byte is used to store small integer values efficiently. They are ASCII characters or small integer values that represent 8-bit signed integers. They are generally used when the work we need to do is small and requires a smaller range of values. 

                                            1 byte = 8 bits 

The range of byte data types lies between -128 and 127. It helps to work in a condition where memory availability is low and working doesn’t require a larger integer data type.

Byte Data type
byte  x= 42, byte y= -12

3. Short Data Type

This data type is 16 bits, which is equivalent to 2 bytes. It ranges from -32,768 to 32,767. The minimum value for this data type is -32,768, and the maximum value is 32,767. These are used when we need a larger data range than byte for integer values. 

Short Data Type
Short x = 1000, short y = -800

4. Int Data Type

The Int data type is the most commonly used primitive data type used in Java. Its value ranges from 2^31 to 2^31-1. It can store up to 32 bits, equivalent to 4 bytes. This data type is the most commonly used integer, representing a whole number.

INT Data type
int  x= 42, int y= -12

5. Long Data Type 

These data types are used to store huge integer values. These data types can store up to 64 bits, equivalent to 8 bytes. The data in the long can range from 2^63 to 2^63-1.  The default value of the long data type is zero. It is used when the range of values needed exceeds those provided by the INT data type.

Long Data type
long  x= 23454L, long y= -20000L

6. Float Data Type

The floating point data type consists of single-precision floating point numbers. They are generally used for decimal values. Their values may range from -3.4 x 10^38 to + 3.4 x 10^38. It can store upto 32 bits of data, equivalent to 4 bytes. The float data type should never be used for precise values like currency. It is set to 0.0F as the default value.

Float Data type
float f1 = 456.78f

7. Char Data Type

The Char data type stores letters, symbols, or another textual element. It represents a single Unicode character. It can contain up to 16 bits, which is equivalent to 2 bytes. 

Char Data type
char char= ‘A’

8. Double Data Type

These data types store double-precision floating point numbers. They are generally used in floating-point calculations. It can range from -1.7 x 10^308 to + 1.7 x 10^308. It can store more than the standard floating data type, which is 64 bits, equivalent to 8 bytes. 

Double Data type
double x = 33.6

Various Data Types In Java

Data Types In Java
Primitive Data Type Size (in Bits) Size (in Bytes) Minimum Range  Maximum Range
byte 8 1 -128 127
short 16 2 -32,768 32,767
int 32 4 -2,147,483,648 2,147,483,647
long 64 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
float 32 4 ~ -3.4 x 10^38 ~ 3.4 x 10^38
double 64 8 ~ -1.7 x 10^308 ~ 1.7 x 10^308
char 16 2 0 (Unicode 0) 65,535 (Unicode 2^16 – 1)
boolean N/A N/A true or false true or false

Secondary Data Types In Java

These data types store the memory addresses or references of the objects instead of the data itself. In Java, secondary data types are objects, arrays, classes, etc. They do not store the actual values of the data.

Recommended Reads

Data Types In Java FAQs

What are primitive data types in Java?

Primitive data types are the fundamental building blocks of data manipulation. Some of the major data types in Java are byte, char, double, int, float, etc.

However, we covered the complete data types in Java in this article. Read that, too.

Is string a primitive data type?

No, string is a non-primitive data type, but it is already predefined in Java, so it creates conflict sometimes.

Can we change the value of the data type of a variable once it is declared?

No, we cannot change the value of a variable once it has been declared with a specific data type. You will need to declare a new variable with the data type and perform the required conversion.

How does Java handle data type conversions?

You can convert between data types in Java using type casting. There are two types of casting: implicit casting and explicit casting.

When converting from a smaller data type to a larger one, implicit casting takes place automatically, whereas explicit casting necessitates a manual process and may cause data loss.

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe
Scroll to Top
close
counselling
Want to Enrol in PW Skills Courses
Connect with our experts to get a free counselling & get all your doubt cleared.