In Java, there are two categories of data types: primitive data types and reference data types. Primitive data types are basic data types that are built into the Java language, while reference data types are objects that are created from classes.
Here are the primitive data types in Java:
Data Type | Size (in bytes) | Range of Values | Default Value | Example |
---|---|---|---|---|
byte | 1 | -128 to 127 | 0 | byte age = 20; |
short | 2 | -32,768 to 32,767 | 0 | short temperature = -10; |
int | 4 | -2,147,483,648 to 2,147,483,647 | 0 | int count = 1000; |
long | 8 | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 0L | long distance = 1000000000L; |
float | 4 | Approximately ±3.40282347E+38F | 0.0F | float pi = 3.14f; |
double | 8 | Approximately ±1.79769313486231570E+308 | 0.0D | double distance = 1000000000.0; |
char | 2 | ‘\u0000’ to ‘\uffff’ (or 0 to 65,535) | ‘\u0000’ | char letter = 'A'; |
boolean | 1 | true or false | false | boolean flag = true; |
Note that the range of values for each data type is dependent on its size. The larger the size, the larger the range of values it can store. Also, remember that the default value is the value assigned to a variable if no value is explicitly assigned.
Primitive Data Types In Java
Primitive data types are basic data types that are not objects.
1.byte:
The byte data type is an 8-bit signed two’s complement integer. It has a minimum value of -128 and a maximum value of 127. The default value is 0.
Here is an example of how to declare and use a byte variable in Java:
byte num = 10; System.out.println(num); // output: 10
2. short:
The short data type is a 16-bit signed two’s complement integer. It has a minimum value of -32,768 and a maximum value of 32,767. The default value is 0.
Here is an example of how to declare and use a short variable in Java:
short num = 1000; System.out.println(num); // output: 1000
3. int:
The int data type is a 32-bit signed two’s complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. The default value is 0.
Here is an example of how to declare and use an int variable in Java:
int num = 100000; System.out.println(num); // output: 100000
4. long:
The long data type is a 64-bit signed two’s complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807. The default value is 0.
Here is an example of how to declare and use a long variable in Java:
long num = 1000000000L; System.out.println(num); // output: 1000000000
Note that you must append an “L” or “l” at the end of the value to indicate that it is long.
5. float:
The float data type is a single-precision 32-bit IEEE 754 floating point. It has a minimum value of 1.4E-45 and a maximum value of 3.4028235E38. The default value is 0.0f.
Here is an example of how to declare and use a float variable in Java:
float num = 3.14159f; System.out.println(num); // output: 3.14159
Note that you must append an “f” or “F” at the end of the value to indicate that it is a float.
6. double:
The double data type is a double-precision 64-bit IEEE 754 floating point. It has a minimum value of 4.9E-324 and a maximum value of 1.7976931348623157E308. The default value is 0.0d.
Here is an example of how to declare and use a double variable in Java:
double num = 3.14159; System.out.println(num); // output: 3.14159
Note that you can also use the letter “d” or “D” at the end of the value to indicate that it is a double, but it is not necessary since double is the default floating point data type.
7. char:
The char data type is a 16-bit Unicode character. It has a minimum value of ‘\u0000’ (or 0) and a maximum value of ‘\uffff’ (or 65,535). The default value is ‘\u0000’.
Here is an example of how to declare and use a char variable in Java:
char letter = 'A'; System.out.println(letter); // output: A
Note that you must use single quotes to represent a char value.
8. boolean:
The boolean data type represents a boolean value, which can be either true or false. The default value is false.
Here is an example of how to declare and use a boolean variable in Java:
boolean flag = true; System.out.println(flag); // output: true
Non-Primitive Data Types in java
Non-primitive data types are also known as reference types because they refer to objects. They include classes, arrays, and interfaces. Unlike primitive data types, non-primitive data types are created by the programmer.
Here is an example of how to declare and use an array variable in Java:
int[] nums = new int[3]; nums[0] = 1; nums[1] = 2; nums[2] = 3; System.out.println(nums[0]); // output: 1 System.out.println(nums[1]); // output: 2 System.out.println(nums[2]); // output: 3
In this example, we declare an integer array variable named “nums
” that can hold three integer values. We initialize the array by assigning values to each element of the array. We then use the System.out.println
method to print the values of the first, second, and third elements of the array.
In summary, Java has eight primitive data types and non-primitive data types. Primitive data types are basic data types that are not objects, while non-primitive data types refer to objects such as classes, arrays, and interfaces. Understanding the different data types in Java is important for writing efficient and effective programs.
Type Casting in Java
Type casting in Java refers to the process of converting a value of one data type to another data type. This can be done explicitly or implicitly.
Implicit Type Casting:
Java allows implicit type casting of smaller data types into larger ones. For example, you can assign an int value to a long variable without an explicit cast, since long can store a larger range of values than int.
Here’s an example:
int x = 10; long y = x; // implicit type casting System.out.println(y); // output: 10
In this example, the value of x is assigned to y without an explicit cast. Since long can store larger values than int, the value of x
is automatically promoted to a long value.
Explicit Type Casting:
Java also allows explicit type casting of larger data types into smaller ones. This is necessary when you want to store a larger value in a smaller variable. However, it may result in a loss of precision or information.
Here’s an example:
double x = 3.14; int y = (int) x; // explicit type casting System.out.println(y); // output: 3
In this example, the double value of x
is cast to an int value using an explicit cast. Since int can only store whole numbers, the fractional part of x
is lost when it is cast to y
.
It is important to note that typecasting should be used carefully, as it can result in unexpected behavior or errors.
Here’s another example:
int x = 130; byte y = (byte) x; // explicit type casting System.out.println(y); // output: -126
In this example, the int value of x
is cast to a byte value using an explicit cast. Since byte can only store values from -128 to 127, the value of x
is truncated to fit into the byte range. This results in a value of -126, which may be unexpected.
In summary, type casting in Java allows you to convert a value of one data type to another data type. Implicit type casting is done automatically by the compiler, while explicit type casting requires the use of the cast operator. However, type casting should be used carefully to avoid unexpected behavior or errors.
FAQ’s
What is a data type in Java?
A data type in Java is a classification of data that specifies the type of values that can be assigned to a variable or used in an expression. Java has two categories of data types: primitive data types and non-primitive (reference) data types.
What is the difference between primitive and non-primitive data types in Java?
Primitive data types are basic data types that are not objects, while non-primitive data types refer to objects such as classes, arrays, and interfaces. Primitive data types are predefined in Java and have a fixed size and range of values, while non-primitive data types are created by the programmer and can vary in size and structure.
What are the primitive data types in Java?
Java has eight primitive data types: byte, short, int, long, float, double, char, and boolean.
What is the default value of a variable in Java?
The default value of a variable in Java depends on its data type. For primitive data types, the default value is 0 or false (for boolean), and for non-primitive data types, the default value is null.
What is typecasting in Java?
Type casting in Java is the process of converting a value of one data type to another data type. It can be done implicitly or explicitly. Implicit type casting is done automatically by the compiler when a value of a smaller data type is assigned to a larger data type. Explicit type casting is done manually by the programmer using the cast operator. For example: int x = (int) 3.14;
casts the double value 3.14 to an int value.
Why is it important to use the correct data type in Java?
Using the correct data type in Java is important for several reasons. It ensures that the program runs efficiently by using the minimum amount of memory necessary to store the data. It also helps to prevent errors and unexpected results caused by data type mismatches.