Data Types in Java Programming

Introduction

In this article, we will take a look at different Data Types in Java. Data Types form the basic building blocks of one’s guide to learn a programming language. Every programming language provides its own set of Data Types (Generally the Same) with different features and functionalities.

Hence, it is important to know about the Data Types in Java Programming for a deeper insight into the language’s core concepts. We will look at different Data Types in Java with detailed explanations and examples to show how we declare, initialize and use a variable.

What is a data type in Java?

Data Type is defined as the type of data that can be stored in a variable. It tells the Java Compiler how a user wants to use the data. Apart from this, a Data Type restricts the values a variable or function might take, along with this it defines how data is stored in memory.

As Java is a strongly typed language, you need to define the data type of the variable to use it and you can not assign incompatible datatype otherwise the compiler will give you an error.

Example of Data Type in Java:

int num = 10;

Here we define a variable num with Data Type int short for Integer. This indicates that the variable num can store only integer values. Hence we assign it the value 10.

If we try assigning values other than integers that it will be incompatible with the variable’s Data Type.

Example:

If we declare a variable like this:

int d="Hello";

The compiler will give you an error with this message – "Type mismatch: cannot convert from String to int".

We get this error because we assign a String to an int type variable. So, in order to assign a String value to a variable, we use String Data Type in Java.

So we define it like this:

String d = "Hello";


Data Types in Java

There are two types of data types in java.

  • Primitive Data Types
  • Reference or Non-Primitive Data Types.

Primitive data types

Primitive data types are those Data Types that are defined by the Java language itself.

There are 8 primitive data types in java.

Data Type Default Value Default size
boolean false 1 bit
char ‘\u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

Now, let’s have a quick look at each datatype.

  • boolean: This Primitive Data Type represents only 1 bit of information, hence has only two permissible values: True or False. A boolean variable can hold any one of the two values at an instant. It is used to represent logical values and is useful in conditional statements (if, else if, etc).

Example: boolean var = true;

Here we define a boolean variable var initialize to value true.

  • char: This data type is used to declare character type variables that can store character values. It uses 2 bytes of space in memory to store each variable. The char range lies between 0 to 65,535 (inclusive).

Example: char i = 'a';

We enter char values in Single quotes. If we assign an integer value to a char type variable, the compiler will implicitly typecast the integer to char by converting it into its corresponding ASCII value.

          So, if we assign char i = 97, then the variable i will hold the value a as the ASCII value of a is 97.

  • byte: This Data Type in Java declares a variable that can hold 8-bit or 1 byte signed (both positive and negative values) two’s complement integer. Its range lies between -128 to 127, if it exceeds either range the compiler will throw an error.

Example:  byte num1 = 127; is a valid declaration.

                                 byte num2 = -129; is an invalid declaration.

  • short: Unlike byte, this Data Type stores a 16-bit signed 2’s complement integer that uses 2 bytes of spaces for each variable. Its range of permissible values lies between -32,768(Minimum) to 32,767(Maximum). In java, it is by default initialized to 0.

Example: short num = 10;

  • int: This data type is used to declare variables that can hold a 32-bit signed two’s complement integer. As a result, it requires 4 bytes of space to store each int type variable. The permissible values of an int type variable lie in the range –2,147,483,648 to 2,147,483,647.

We had discussed the example above in the article. There is one interesting feature about int in Java. If we assign a character value to an int type variable the compiler will implicitly convert it to int type and will assign the corresponding ASCII value.

Example: int var = 'A';

In this case, var will hold the value 65 as A in ASCII code has value 65. This feature of implicit typecasting is also available with short and long data types.

  • long: This data type is used when we have to store large integral values. It can represent a 64-bit signed two’s complement integer and requires 8 bytes of space for each variable.

Example: long num = 10000000000000L;

Here, when we assign a value greater than the Integer range in Java(-2,147,483,648 to 2,147,483,647) to a long data type variable we must provide it with an L as declared above in the example. This tells the compiler that the value is of a long type. If you assign it like this:

long num = 10000000000000;

The compiler will give an error: integer number too large as when we hard code a value, Java does not know whether it is a long type value. It treats all numbers the same.

  • float: This data type is used to represent fractional or decimal numbers. It uses 4 bytes of space in memory with a 32-bit IEE-754 Floating-Point standard. In Java, the float type variable can store values up to 5 decimal places. If we assign a value having more than 5 numbers in the fractional part, then it is rounded off to its first 5 places.

Example: float num = 10.567F;

We declare float variables with suffix F or f to instruct the compiler that it is a float variable similar to what we saw in examples of long datatype. If we declare it without the suffix like this:

float num = 10.567;

Then the compiler will throw an error: possible lossy conversion from double to float. The reason being, in Java any decimal number you assign to a variable is by default considered a double literal or value and we are trying to assign a double value to a float type.

  • double: This primitive data type is also used to represent decimal or fractional numbers but has more precision than float type. Following the 64-bit IEEE-754 Floating-Point Standard, this datatype can store values with precision up to 12 decimal places. It uses 8 bytes of space for each variable. So it is useful in maintaining precision for large decimal values.

Example:double num = 10034.5674546;

Why is the size of char 2 byte in java..?

In Java, the size of char is 2 bytes as opposed to programming languages like C/C++ which have allocated only 1 byte of space for char type variables. The reason is Java supports Unicode Characters and uses UTF-16 to encode its characters rather than the ASCII Code System. When Java was developed it was anticipated that any Unicode Character would require 16 bits or 2 bytes of memory for storage, these Unicode are used to represent special symbols. Hence, char has size 2 bytes in Java.

Now, Let’s see some practical examples of Primitive data types:

Adding two integers

When you run the above program, you will get the below output:

30

Assigning int to double(Widening)

Here we will assign an int to double. As double-takes more memory than an int. This is a widening operation.

When you run the above program, you will get the below output:

30
30.0

Assigning double to int(Narrowing or typecasting)

Here we will assign double to int. As double-takes more memory than an int. This is a Narrowing operation.

When you run the above program, you will get the below output:

30.0
30

Assigning int to byte(Overflow condition)

When you assign an int to byte and the value of int is larger than the size of byte then it is a case of overflow.

When you run the above program, you will get the below output:

200
-56

Reference or Non- Primitive data types:

In simple terms, Reference data types are those data types that are provided as a class by Java API or by the class that you create. So, a variable of class type is a Reference Data Type or Non-Primitive Data Type. It is not always a built-in feature of Java, however, there is an exception.

The String class is an example of a Reference data type provided by java and it is not a primitive type as it provides inbuilt methods too.

Every time we create a String variable as an object or literal, an object is created internally, and when we assign it to a variable it becomes a Reference Data Type. The array is also an example of Non-Primitive Data Types, as we have to create an object while defining an array.

Java by default allocates 8 Bytes of space for each reference type variable we create.

To summarize the Data Types in Java:


What is the difference between Primitive and Non-Primitive Data Types?

Now let’s have a quick look at the key differences between Primitive and Non-primitive or Reference Data Types.

Primitive Data Types Non-Primitive or Reference Data Types
1. It is the built-in Data Type that is predefined by Java itself. 1. It is the user-defined datatypes and not predefined by Java except the String Class.
2. Java allocates different sizes or bytes for each Primitive Data Type. The size of each Primitive Data Type differs with their type. 2. Java allocates fixed size (8 bytes) for each Non-Primitive Data Type.
3. It does not provide any inbuilt methods to invoke for performing operations on the data. 3. We can invoke or call methods, even define our own methods to perform operations.
4. It always has a value to it and cannot be null. 4. It can have a null value.
5. It begins with a lowercase letter. Examples: int, char, float, double, etc. 5. As a reference data type, it must begin with Uppercase Letter because all classes in Java follow the same convention. Examples: Array, String, Object Class, and all User-defined Classes.
6. While creating a Primitive Data Type, we assign a value to it. 6. Here, we assign a reference to the variable.
So that’s all about Data Types in Java Programming.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *