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:
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.
1. Variable x can store an integer number like 100 as:
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)
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.
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:
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.
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.
3. Range of values
Integer data types in Java
2. Byte data type is an 8-bit signed two’s complement integer.
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.
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.
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:
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.
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:
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.
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:
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|
|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|
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.
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.
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.
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.
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:
Table 2: Size and Range of Floating point Data types
|Type||Size||Minimum value||Maximum value|
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.
Let’s take an example program based on the character data type.
Program source code 6:
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.
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:
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.
2. int a, b, c; // Initialization.
Output: o, o, o
3. int a=20, b, c;
Output: 20, 0, 0
4. int a=10, b=20, c;
Output: 10, 20, 0
5. int a=10, b=20, c=30;
Output: 10, 20, 30
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
⏪ PrevNext ⏩