What are Data Types in Java?
- Data types in Java specify how memory stores the values of the variable. Each variable has a data type that decides the value the variable will hold. Moreover, Primitive Data Types are also used with functions to define their return type
Data types in Java are categorized into two main groups: primitive and non-primitive (reference) data types.
Primitive Data Types
- These are the basic data types that represent single values. There are eight primitive data types in Java:
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;
There are 8 primitive data types. They are depicted below in tabular format below as follows:
Top comments (0)