What is Data type?
There are different types of data out there in the world. In this case, different kinds of data allocate different spaces in the hard disk. Therefore, according to the definition, to assign memory for the variables for different information types is called Data type.
In an easy word, the data type for letters and numbers will be different. We must write the data type first, and then we should assign one variable name. Data types can be of two kinds, primitive data type, reference/object data type.
Primitive Data type
Java Program has already pre-defined some data types is known as the Primitive data type. These data types are already allocated in the Java language. There is eight Primitive data type. They are,
Byte
The byte is an eight-bit signed two’s complements integer. The main difference between the byte and integer is that the integer can store more significant value than that of the byte. However, a byte can have a maximum value of 127 (2^7 – 1) and a minimum value of -128 (2^7). By default, the initial value of a byte is 0.
Short
Short is a 16-bit both signed two’s complement number of an integer. It has the maximum value of 32,767 (2^15 – 1) and a minimum value of -32,768 (-2^15). The short data type can also be used instead of byte and integer.
Int
Int or integer is a 32-bit both signed two’s complement number of an integer. It has the maximum value of 2,147,483,647 (2^31 – 1) and a minimum value of -2,147,483,648 (-2^31). The initial it has as the default value is 0.
Long
Long is a 64-bit both signed two’s complement number of an integer. It has the maximum value of 9,223,372,037,854,775,807 (2^63 -1) and a minimum value of 9,223,372,037,854,775,808 (2^63). The default value of long data type si 0L. When you need to store any values that go beyond the integer, you can use long as the data type.
Float
Float is single-precision 32-bit IEEE 754 floating-point. The default value for the float data type is 0.0f. If we need to store floating-point values, we can use this data type. Remember if you didn’t have floating-point values to store, still you can use float. But the output will come with the floating-point.
Double
Double is the 64-bit double-precision IEEE 654 floating-point. Usually, the default value is 0.0d. It is used as the default data type for decimal values.
Boolean
Boolean data type represents one bit of the information such as true and false. In this case, there are only two possible values for the Boolean data type. The default value is false. The boolean data type is usually used instead of a simple flag of true/false condition.
Char
Char/ character is a single 16-bit Unicode character. It has the maximum value of “\uffff” ( 65,535) and the minimum value of “\u0000” (0). Char data type is used to store any character/letters.
Reference Data type
Reference Data type is just the opposite of primitive data type. As we have said, the primitive data type is pre-defined in Java. On the other hand, the reference data type can be manually defined in Java. This type of data type is created using the constructors of the class. For example,
Fruit orange1 = new Fruit (“Orange”);
Here in the part of the program, the Fruit is the reference data type, and orange1 is the variable.
Following is an example showing all the data type together in one page. Check out the output. Try it in your Java compiler.
/* Java variables and data type example */
package variabledatatype;
public class VariableDataType {
public static void main(String[] args) {
boolean b = true; // Dynamic Initialization
char c = 'm';
long l = 9993332;
short s = 8453;
// byte b = 6;
int i = 323;
float f = 7.4f;
double d = 1.00;
System.out.println("boolean b = " + b);
System.out.println("char c = " + c);
System.out.println("long l = " + l);
System.out.println("short = " + s);
System.out.println("integer i = " + i);
System.out.println("float f = " + f);
System.out.println("double d = " + d);
}
}