Exploring Java Data Types: A Comprehensive Guide for Beginners
Bhavishya Ambati
Ex-Student Mentor at IEEE SB NBKRIST| Ex-Hyderabad Section Co-Lead for IEEEXTREME 18.0| Google Cloud Arcade| Postman API | Salesforce AI Associate| Infosys Pragati: Path to Future Cohort-3| 50+ Skill Badges| iQonnect'24
Introduction:
Data types are the kind of data that variables hold in a programming language. A data type is an attribute of data that tells the compiler or interpreter how the programmer plan to use the data. There are two types of data types in Java:
1. Primitive data types ( intrinsic or built-in types )
2. Non-primitive data types (derived types )
1. Primitive Data Types in Java:
A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and it has no additional methods.
Primitive data types in Java are the foundational building blocks of data manipulation. Unlike more complex data structures, they represent simple values such as a single number or character. Java offers eight primitive types: byte, short, int, long, float, double, char, and boolean
There are 8 types of Java primitive data types namely:
a. Byte
b. Short
c. Int
d. Long
e. Float
f. Double
g. Boolean
h. Char
Primitive data types of Java are the most basic and are considered building blocks of data manipulation. They are predefined single values with no special capabilities. The size of Java primitive data types doesn’t change with any changes in the operating system, as Java is independent of all operating systems.
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.
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?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;
Boolean:
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.
2. Non-Primitive Data Types in Java
Non-primitive data types in Java are also known as reference types because they refer to objects. These are the datatypes that have instances like objects.
Those data types that are not predefined in the Java and are created by the programmers such as Strings, Arrays, Classes are called non-primitive data types. They are also known as reference types because they refer to objects.
Array
An array holds elements of the same type. It is an object in Java, and the array name (used for declaration) is a reference value that carries the base address of the continuous location of elements of an array.
Example:
int Array_Name = new int[7];
String
The String data type stores a sequence or array of characters. A string is a non-primitive data type, but it is predefined in Java. String literals are enclosed in double quotes.
class Main {
public static void main(String[] args) {
// create strings
String S1 = "Java String Data type";
// print strings
System.out.println(S1);
}
}
Class
A class is a user-defined data type from which objects are created. It describes the set of properties or methods common to all objects of the same type. It contains fields and methods that represent the behaviour of an object. A class gets invoked by the creation of the respective object.
There are two types of classes: a blueprint and a template.?For instance, the architectural diagram of a building is a?class, and the building itself is an?object?created using the architectural diagram.
Example:
Interface
An interface is declared like a class. The key difference is that the interface contains abstract methods by default; they have nobody.
Example:Example:
interface printable {
void print();
}
class A1 implements printable {
public void print()
{
System.out.println("Hello");
}
public static void main(String args[]) {
A1 obj = new A1();
obj.print();
}
}
Enum
An enum, similar to a class, has attributes and methods. However, unlike classes, enum constants are public, static, and final (unchangeable – cannot be overridden). Developers cannot use an enum to create objects, and it cannot extend other classes. But, the enum can implement interfaces.
//declaration of an enum
enum Level {
LOW,
MEDIUM,
HIGH
}
Differences Between Primitive and Non-Primitive Data Types
The main differences between primitive and non-primitive data types are listed below.
Conclusion
Data types are the basis of programming languages. It is essential to know about Java data types before moving to the advanced concepts of Java. Understanding data types will help you create a simple program or develop any application or software.