This tutorial is designed for both beginners and intermediates to grasp the idea of data types and the classification of data types in the Java programming language. Besides these, you will also explore one of the important processes associated with data types- the java typecasting or data type conversion.
Data types in any programming language specify the behavior of the data the programmer needs. A data type tells the compiler the type or the size of the data to be stored in a variable or identifier to perform the specific operation. So in a sense, defining a data type to a variable restricts freedom of the variable to take any values.
As java is a statically typed programming language, the significance of declaring a variable with the appropriate data type before use is pretty notable. Let's take an example
int age;
Here age
is a variable of data type int which indicates the variable age
should only hold integer values.
Now let’s explore the different types of java data types.
In java, data types are broadly classified into two:
From the above diagram, we have seen the 8 primitive data types in java. These data types are in-build data types that further can’t be divided. Hence is referred to as primitive data types. Let’s explore each one of the data types in detail.
Mathematically, an integer number is a number that does not contain any fractional part. Integers can be a positive number or negative number or even 0
, together account as whole numbers. For instance, -20
, 0
, 99
etc are integers while 8.5
, ¾
etc are not.
Data types like byte
, short
, int
and long
are part of integer data types.
long
variable from an int
variable, always end the long variable value with letter ‘L’. For instance, 1000000000L.Examine the below example to understand how integer data types work in a program.
public class DatatypeEx {
public static void main(String[] args) {
byte bnum = 126;
System.out.println(bnum); // prints 126
short snum = -32768;
System.out.println(snum); // prints -32768
int inum = 999999999;
System.out.println(inum); //prints 999999999
long lnum = 9999999990 L;
System.out.println(lnum); //prints 9999999990
}
}
Floating-point numbers are those numbers that have fractional parts like 3.14, 6/7, etc. In java, we have two primitive data types that accept numbers with decimal points. They are :
Data Types | Data Size (bits) | Data Size(bytes) | Default value |
---|---|---|---|
float | 32 bits | 4 bytes | 0.0f |
double | 64 bits | 8 bytes | 0.0d |
public class DatatypeEx {
public static void main(String[] args) {
float num1 = 9.9 f;
System.out.println(num1); // prints 9.9
double num2 = -3.14;
System.out.println(num2); // prints -3.14
float num3 = 9.999990000000000009990;
System.out.println(num3); //prints error message
}
}
Character data types deal with characters. Unlike other programming languages, Java uses a Unicode system, and hence char
data type represents a 16 bit Unicode character. The default value of char type is ‘\u0000
’ which is the minimum value of a Unicode system. The maximum value a char type can take is ‘\uffff
’.
Data Types | Data Size (bits) | Data Size(bytes) | Default value |
---|---|---|---|
char | 16 bits | 2 bytes | 'u0000' |
If you are interested to know more about why java uses the unicode system,visit the unicode system of java.
public class DatatypeEx {
public static void main(String[] args) {
char ch1 = '\u0045';
System.out.println(ch1); // prints E
char ch2 = '\u0065';
System.out.println(ch2); // prints e
}
}
Boolean is a special data type that holds any of one possible value, either True or False. This data type is usually used while evaluating conditions or comparison or logical or relational operations.
Data Types | Data Size (bits) | Data Size(bytes) | Default value |
---|---|---|---|
boolean | 1 bit | false |
public class DatatypeEx {
public static void main(String[] args) {
boolean b1=(6>9)?True:False;
System.out.println("Is 6 greater than 9: "+b); // prints False
boolean b2=(9>3)?True:False;
System.out.println("Is 9 greater than 3: "+b); // prints True
}
}
As already stated, non-primitive data types are not basic data types, they are data types derived from the primitive data types based on the program requirement. In other ways, we can say non-primitive data types are the user-created data types. Since non-primitive data types are not defined by java the size of the data type is not fixed, it can be of any length. Unlike primitive data types, a non-primitive data type starts with an uppercase letter. For instance, String
, Array
, Class
, etc. Another feature that differentiates a primitive data type and the so-called Reference data type is that the non-primitive data types can hold null values.
In upcoming tutorials, you will learn about these non-primitive data types like String, Arrays, Class, Interface, etc.
Like many other programming languages, java also supports data type conversion. As its name suggests, generally we can state type conversion or typecast as the process of converting one data type into another data type. For instance, we can convert an integer data type to float and vice versa. The conversion can be done manually by the programmer or automatically by the compiler.
Based on the conversion strategy, in Java, we have 13 types of typecasting. Out of the 13 types, we will discuss here the two important types of casting, namely:
If you are interested in learning more about typecasting, visit different kinds of type conversion in java.
Widening Typecasting is the process of automatically converting a lower data type (int
)into a higher data type(double
). Since this conversion is done automatically by the compiler it is also referred to as Automatic conversion or Implicit conversion.
public class DatatypeConv {
public static void main(String[] args) {
// creating integer type variable
int inum = 36;
System.out.println("Integer Value :"+ inum);
//converting integer to float type variable
float fnum = inum;
System.out.println("Float Value :"+fnum);
}
}
Output:
Integer Value :36 Float Value :36.0
Explanation:
The above code snippet shows the conversion of an integer number to a floating-point number. Since a float
is a higher data type of int
, no specific declaration of datatype was required at the time of conversion. The compiler automatically converts the value to a higher data type, in our case to float
.
From this, we can infer that widening type casting occurs in situations when the data types are compatible with each other and the size of the target data type is higher than the source. Widening type casting ensures the data without any loss.
Narrowing Typecasting is the process of converting a higher data type (double
)into a lower data type(int
). Since this conversion is done manually by the programmer it is also referred to as Explicit conversion or casting.
public class DatatypeConv {
public static void main(String[] args) {
// creating float type variable
float fnum = 14.7f;
System.out.println("Float value :" +fnum);
//Converting float value to int value
int inum = (int)fnum;
System.out.println("Integer value :" +inum);
}
}
Output:
Float value :14.7 Integer value :14
Explanation:
Here we converted a higher data type value to a lower data type value which implies narrowing a data type. Hence we need to explicitly specify the data type to which it needs to be converted, in our case to int
.
Narrow-type casting is just the opposite of widening casting. Since the size of target data is less than the source data, the chance to lose data is significantly high. Hence type casting has to be done manually else the compiler reports a compile-time error.