Data Types in Java


We know that we need a variable to store the data. Internally, a variable represents a memory location where data is stored. When we use a variable in java program, we have to declare first it as:
         int x;
Here, "x" is a variable that can store int (integer) type data. It means that int represents the nature of data which can be stored into x. Thus, int is also called data type in java.

Basically, data type in java is a term that specifies memory size and type of values that can be stored into the memory location. In other words, data types define the different values that variable can take.
For example:
1. Variable x can store an integer number like 100 as:
     x=100;
Here, = represents that value 100 is stored into x.

2. String name="Deep"; // Here, String is a data type and name is a variable which can take only string values.
    
3. int num=10; // Here, int is data type and num is variable which can only take integer values like 10, 20, 30, 40 and so on. The semicolon is used to end a particular statement in java like pull stop in the English language so that java will know that statement is completed.

Java language provides several data types as shown in the below figure. All data types are divided into two categorized. They are as follows:
1. Primitive data types (also called intrinsic or built-in types)
2. Non-primitive data types (also called derived or reference data type)
Classification of data types in java
Numeric data types are also called arithmetic data types in java.

Primitive Data types in Java


Primitive data types are those data types whose variables can store only one value at a time. You cannot store multiple values of the same type. These data types are predefined in Java. They are named by a Keyword.
For example:
         int x; // valid
         x=10; // valid because "x" store only one value at a time because it is the primitive type variable.
         x=10, 20 ,30 40; // invalid
Primitive data types are not user-defined data-types. i.e. Programmers cannot develop primitive data types.

Types of Primitive data types in Java

Java defines eight primitive data types: boolean, char, byte, short, int, long, char, float, and double. These can be further categorized into four groups. They are as follows:

1. Conditional category data type: boolean               
2. Character category data type: char
3. Integer category data types: byte, short, int, and long.
4. Float category data types: float and double
All the data types are the keywords which are predefined in Java. They are in small letters.

Now it is important to understand the memory limitations which decides which data type should be used for a particular number. For example,  when you define the age of a person, the age of any person will not cross 120.
In this case, using short data type is enough instead of using long which will take a big memory. Therefore, you will have to understand the important terms for every data types.

1. Memory size allocated

Each data type has some memory size defined in Java. Whenever a variable is declared with a data type, the memory size is automatically defined in the RAM by the JVM.
For example:
       If we declare int a; then the size of the memory is defined as 4 bytes.

2. Default value

Every primitive data type has default values defined in java. When the programmer does not declare any value to the variables, default values will be assigned by the JVM during the object creation.
For example:
     The default value of int is 0 (zero).
     The default value of the byte is 0.

3. Range of values

The range of value represents min or max value that a data type can hold. When we assign the value of a variable which is not fit in the range of data type, error or exception will be thrown.
For example:
       The range of int data type is between the minimum value -2,147,483,648(2^31) and the maximum value +2,147,483,648(2^31-1).
👉 All the data types represent both positive and negative numbers. In the programming language, Zero is considered as a positive number.

Integer data types in Java


These data types represent integer number without any fractional parts or decimal points. For example, 225, -56524, 0, 1045, etc. come under this category. Integer data types are again subdivided into four types: byte, short, int, and long as shown in below figure.
Integer data types in java
👉 All java values are signed types (positive or negative). Java does not support unsigned types. Many other computer languages including C/C++ support both unsigned and signed integers.

byte

1. A byte is the smallest integer data type which has the least memory size allocated and it is mostly used to save memory.

2. Byte data type is an 8-bit signed two's complement integer. 
3. The memory size of byte type is 8-bit i.e 1 byte.

4. A byte keyword represents from 0 to 127 on the positive side and from -1 to -128 on the negative side. So it can represent the total 256(2^8) numbers.

5. Default value of byte is 0.
6. Variables of type byte are especially used to flow a stream of data from network or file.
For example:
A variable of type byte can be declared by using byte keyword as shown in the below example.
      byte b, c; // Variables b and c of type byte.
Let's take one example program related to byte.
Program source code 1:
    package datatypePrograms; public class ByteExample { public static void main(String[] args) { byte num=100; // byte is 8 bit value. System.out.println(num); } }
    Output: 100
In the preceding example program, we have declared byte data type for variable num with value 100 which is stored into num. Byte represents any value between -128 to 127.
But when you will assign 150 value at the place of 100 to variable num, you would get compilation problem: Type mismatch error: cannot convert from int to byte because the value is out of the range of byte type. The range of byte is -128 to +127.

short

1. Short data type has greater memory size than byte and less than Integer.
2. A short data type is a 16-bit signed two's complement integer.
3. The default memory size is allocated 16 bit i.e 4 bytes.
4. It represents 0 to 32767 on the positive side and on the negative side, from -1 to -32768. So it can represent a total of 65536(2^16) numbers.
5. The default value is 0.


Let's see an example program.
Program source code 2:
    package datatypePrograms; class ShortExample { public static void main(String[] args){ short num=200; // Here, 200 is stored into num which is declared as short type. System.out.println(num); } }
    Output: 200
We cannot use byte data type in the above example because a byte cannot hold 200 value but short can hold the value 200 because of its wider range.

int

1. This data type is mostly used for integer values in the Java programming.
2. Int data type is a 32-bit signed two's complement integer.
3. It has wider range from -2,147,483,648 to 2,147,483,648.
4. The memory size is 32 bits i.e 4 bytes and default value is 0.

Let's create a program where we will store two values into a and b with data type int.
Program source code 3:
    package datatypeProgram; class IntegerExample { public static void main(String[] args){ int a=200; // Here, 200 is stored into a which is declared as int type. int b=300; // 300 is stored into b which is declared as int type. System.out.println(a+b); } }
    Output: 500

long

1. This data type is mostly used for a huge number where int type is not large enough to store the desired value. 
2. A long data type is a 64-bit signed two's complement integer.
3. Default memory size allocated to this data type is 64 bits i.e 8 bytes and default value is 0.
4. It has wide range from -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,808(-2^63-1). Long data type is useful when big whole numbers are needed.
For example:
               long num=-2334456L;
Here, -2334456 has been stored into num with type long. L represents that JVM will consider it as a long value and will allot 8 bytes to it.

Let's create a program where we will calculate the distance traveled by light in 1000 days using long data type. Let's see the following source code.
Program source code 4:
    package datatypePrograms; public class LongExample { public static void main(String[] args) { int lightSpeed; long days; long seconds; long distance; // Speed of light in miles per sec. lightSpeed=186282; days =1000; // Number of days. seconds=days*24*60*60; // Convert into seconds. distance= lightSpeed*seconds; System.out.println("In 1000 days, distance traveled by light: " +distance + " miles"); } }
    Output: In 1000 days, distance traveled by light: 16094764800000 miles
It is clear from the above example program that the distance value could not have been held in an int variable.

Table 1: Size and Range of Integer Data types

Type Size Minimum value Maximum value
byte One byte -128 127
short Two bytes -32, 768 32, 767
int Four bytes -2, 147, 483, 648 2, 147, 483, 647
long Eight bytes -9, 223, 372, 036, 854, 775, 808 9, 223, 372, 036, 854, 775, 807

👉 Whenever possible you should use smaller data types because wider data types take more time for manipulation. For example, if you want to use int variable to store 50 value, it is advisable that you should use a byte variable instead of using int variable because it will improve the speed of execution of program and takes less memory size.

Floating Point Types


Floating-point types are useful to hold numbers containing decimal point or fractional part. For example, 3.14, -2.567, 0.00034, etc. are called floating-point numbers. There are two kinds of floating-point types: float and double.
Floating point data types in java

float

1. A float data type is used to represent the decimal number which can hold 6 to 7 decimal digits.
2. It is used to save the memory in large arrays of floating-point numbers.
3. The float data type is a single-precision 32-bit IEE 754 floating-point.
4. The default memory size allocated for this data type is 32 bits i.e 4 bytes and default value is 0.0f.

For example:
       float num=10.6f; 
Here, if you do not write "f'' then JVM will consider as double and would have allotted 8 bytes. In this case, it will give an error " Type mismatch: cannot convert from double to float". But if we use f, JVM will consider it as float value and allot only 4 bytes.

double

1. A double data type is also used to represent decimal number up to 15 decimal digits accurately. 
2. The double data type is a double-precision 64-bit IEE 754 floating-point.
3. Memory size is 64 bits i.e 8 bytes and default value is 0.0d.
For example:
          double num=1345.6;
          double distance=1.50e9; // Here, e represents x  10 to the power. Hence, 1.50e9 means 1.50*10^9. It is called scientific notation of representing number.

Let's make a program where we will use double variables to calculate area of circle.
Program source code 5:
    package datatypePrograms; public class Area { public static void main(String[] args) { double pi, r; r=5.5; // Radius of circle. pi=3.1416; // Calculate the area of circle. double area=pi*r*r; System.out.println("Area of circle: " +area); } }
    Output: Area of circle: 95.0334

Table 2: Size and Range of Floating point Data types

Type Size Minimum value Maximum value
float Four bytes 3.4e-038 3.4e+038
double Eight bytes 1.7e-308 1.7e+308

Character data type

1. A char data type is mainly used to store a single character like P, a, b, z, x, etc.
2. It is a single 16-bit Unicode character.
3. Memory size taken by a single char is 2 bytes.
4. It can represent a range of 0 to 65536 characters.
5. A default value for char is '\u0000' which represents blank space or single space. Here, '\u' represents that character is a Unicode.
For example:
          char ch='D';

Let's take an example program based on the character data type.
Program source code 6:
    package datatypePrograms; public class CharExample { public static void main(String[] args) { char ch1, ch2; ch1=88; ch2='R'; char ch3; ch3='A'; ch3++; System.out.println(ch1); System.out.println(ch2); System.out.println(ch3); } }
    Output: X R B
In the above example program, ch1 is assigned value 88 which is ASCII value and specifies letter X. ch3 is assigned value A and then it is incremented. So, ch3 will now store B, the next character in the ASCII sequence.

Boolean Data types

1. boolean data type represents one bit of information as either true or false. i.e. there is only two possibles value true or false. Internally, JVM uses one bit of storage to represent a boolean value.
2. It is generally used to test a particular conditional statement during the execution of program.
3. Boolean data type takes zero bytes of memory. 
4. Default value is false.
For example:
       boolean b=false;

In the above all examples, we assigned a value of the variable, assigned value will be printed as output. Suppose if you do not assign the value of the variable, JVM will assign the default value and the default value will be print. Let's see the program source code.
Program source code 7:

    package datatypePrograms; public class DefaultExample { // Declaration of instance Variables. int a; char b; float c; boolean d; // Static method or main method. public static void main(String[] args){ // Create the object of the class. DefaultExample obj=new DefaultExample(); // Call the variable and print it. System.out.println(obj.a); System.out.println(obj.b); System.out.println(obj.c); System.out.println(obj.d); } }

    Output: 0 \u0000 (represents blank space) 0.0 false

Why take boolean data types zero bytes of memory?


Boolean data type takes zero bytes of memory space because boolean data type in Java is implemented by Sun Micro System using the concept of a flip-flop. A flip-flop is a general-purpose register which stores one bit of information (one for true and zero for false).

Different ways to initialize values and output

1. int a=10; // Initialization.
    System.out.println(a);
     Output: 10

2. int a, b, c; // Initialization.
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
         Output: o, o, o

3. int a=20, b, c;
     System.out.println(a);
     System.out.println(b);
     System.out.println(c);
        Output: 20, 0, 0

4. int a=10, b=20, c;
      System.out.println(a);
      System.out.println(b);
      System.out.println(c);
      Output: 10, 20, 0

5. int a=10, b=20, c=30;
      Output: 10, 20, 30

Final words
Hope that this tutorial has covered almost all important points related to primitive data types in java with example programs. I hope that you will have understood this topic clearly and enjoyed it. In the next tutorial, we will learn non-primitive data types in java.
Next ➤ Non-primitive data types

⏪ Prev Next ⏩