Data types in Java with examples
-
Last Updated: July 1, 2023
-
By: javahandson
-
Series
Data types in Java are used to know the type of variables. The variable can be of any type like numeric or non-numeric or boolean etc. Data types also help to assign a sufficient amount of memory to the variable.
Ex. int result = 50;
Explanation
The result is a numeric field as we have made use of the int data type.
The int data type will allocate 4 bytes of memory to the result variable
Java data types are classified into 3 types:
These are developed by programming language developers. Java developers have created java data types for us. Predefined data types are provided to us by default. We just have to make use of them. Predefined data types allow us to store only a single value it does not allow us to store multiple values.
Java provides 8 predefined data types which are categorized into 4 categories :
This data type allocates sufficient memory for integer data in the main memory of the computer. The integer data type contains numeric data without decimals.
Integer category contains 4 subtypes:
byte – This data type allocates 1 byte of memory to the variable.
Range = -127 to 128
short – This data type allocates 2 bytes of memory to the variable.
Range = -32,768 to 32,767
int – This data type allocates 4 bytes of memory to the variable.
Range = -2,147,483,648 to 2,147,483,647
long – This data type allocates 8 bytes of memory to the variable.
Range = -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Formula : (-2^n) to (2^n – 1) byte : 8 bits n = 8-1 = 7 = -2^7 to (2^7) – 1 = -128 to 127 int : 32 bits n = 32-1 = 31 = -2^31 to (2^31) – 1 = -2,147,483,648 to 2,147,483,647 As a developer we have to choose appropriate data type. Ex. byte b = 128; // This will throw an exception because the range of byte is from -128 to 127
This category of data type allocates sufficient memory for real constant numbers in the main memory of the computer. Float data type contains numeric data with decimals.
The float category contains 2 subtypes:
float – float data type allocates 4 bytes of memory to the variable. float data type stores the value in such a way that after the dot(.) it takes 7 decimal places. float value should end with the letter f else we will get compile time error.
Range [ 3.4e−038 to 3.4e+038 ] Ex. float f = 2.5f;
double – double data type allocates 8 bytes of memory to the variable. double data type stores the value in such a way that after the dot(.) it takes 15 decimal places. double value may end or may not end with the letter d, it is optional.
Range [ 1.7e−308 to 1.7e+308 ] Ex. double d = 2.5 or double d = 2.5d;
Note* If we use any real constant value in our program then by default it will be double data type.
This category of data types allocates sufficient memory for single-character data. This category contains only one data type i.e. char.
char data type in java allocates 2 bytes of memory to the variable.
Ex. char ch = 'a';
Java programming language is available in 18 international languages. Every international language will have its own set of characters and symbols. If we are using only 1 byte of memory for char then we have to use an ASCII set where we can store only 256 (2^8) characters which is very less for 18 international languages. Hence for Java, we are using a Unicode char set and it requires 2 bytes of memory where it can store 65536 characters (2^16).
This category contains only one data type i.e. boolean. The purpose of this data type is to store logical values i.e. true or false. This data type contains only 1 bit of information hence no size is defined for it.
Ex. boolean result = true;
When declaring a variable if we do not assign any value to the variable then JVM will provide some default values to that variable.
byte - default value is 0 short - default value is 0 int - default value is 0 long - default value is 0 float - default value is 0.0 double - default value is 0.0 char - default value is \u0000' (nothing that is equivalent to null) boolean - default value is false
Below is the sample program to display the default values of predefined data types
package com.javahandson; public class DefaultValues { byte b; short s; int i; long l; float f; double d; char ch; boolean bool; public static void main(String[] args) { DefaultValues defaultValues = new DefaultValues(); System.out.println("byte default value is : "+defaultValues.b); System.out.println("short default value is : "+defaultValues.s); System.out.println("int default value is : "+defaultValues.i); System.out.println("long default value is : "+defaultValues.l); System.out.println("float default value is : "+defaultValues.f); System.out.println("double default value is : "+defaultValues.d); System.out.println("char default value is : "+defaultValues.ch); System.out.println("boolean default value is : "+defaultValues.bool); } } Output : byte default value is : 0 short default value is : 0 int default value is : 0 long default value is : 0 float default value is : 0.0 double default value is : 0.0 char default value is : boolean default value is : false
The derived data type is composed of primitive data types. Derived data types include Arrays, Pointers, Structures, Union etc. Pointers are not supported by Java also Java has no complex data types like structures or unions. So for Java, we are left with Arrays.
Array helps us to store multiple values of the same data type in a single variable. It is also known as a homogeneous data structure as it contains only homogeneous data.
int[] arr = {20, 30, 70, 80}; // Valid as it only contains integer values. int[] arr = {20, "Java", 70, true}; // Invalid as it contains string as well as boolean values.
These are the data types created by a user or a developer. User-defined data types allow us to store multiple values of the same data types or different data types. In Java, we can use features like Class, Interface or Enumeration to create user-defined data types.
Below is the sample program to show a user-defined data type i.e class
package com.javahandson.oops; public class Student { int studentId; String name; String standard; public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStandard() { return standard; } public void setStandard(String standard) { this.standard = standard; } }
Note* The string data type is a non-primitive data type but it is also predefined in Java. A string data type is used to store the sequence of characters.
String Class inherits the Object Class and also contains different methods like concat, contains, length, etc. Only a non-primitive type can extend another class and contain methods. Hence String is a pre-defined data type but a non-primitive one.
So this is all about different types of data types in Java. I hope you like the article. If you are having any questions on this topic please raise them in the comments section.