A data type in Java refers to the type of data that a variable can hold. Java is a statically typed language, which means that every variable must have a declared type at compile time.
This allows the compiler to perform type checking and ensure that the program is free of type errors. In Java, there are two categories of data types: primitive and reference types.
Primitive Data Types
Primitive data types are the building blocks of Java programming language. They are predefined by the language and represent basic data types. There are eight primitive data types in Java:
- byte: It is used to store whole numbers from -128 to 127.
- short: It is used to store whole numbers from -32,768 to 32,767.
- int: It is used to store whole numbers from -2,147,483,648 to 2,147,483,647.
- long: It is used to store whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
- float: It is used to store fractional numbers with single-precision floating-point precision.
- double: It is used to store fractional numbers with double-precision floating-point precision.
- char: It is used to store a single character or ASCII values ranging from ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535).
- boolean: It is used for variables that can hold either true or false.
Each primitive data type has a default value and a specific range of values that it can hold. For example, the default value of an int is 0, and it can hold whole numbers from -2,147,483,648 to 2,147,483,647.
Reference Data Types
Reference data types are not predefined by the language like primitive types. They are created by the programmer using classes or interfaces defined in Java. Some common examples of reference types include:
- String: It is used to store a sequence of characters.
- Array: It is used to store multiple values of the same type in a single variable.
- Class: It is used to create objects that encapsulate data and methods.
The main difference between primitive and reference types is how they are stored and accessed in memory. Primitive types are stored directly on the stack memory, whereas reference types are stored on the heap memory and accessed through references.
In Java 10 and later versions, type inference allows you to omit the explicit declaration of a variable’s type when it can be inferred from the context. For example, instead of writing:
String name = "John";
You can write:
var name = "John";
The compiler will automatically infer that name is of type String.
Type Conversion (Casting)
Java supports type conversion, also known as casting, which allows you to convert a value of one data type to another. There are two types of casting: implicit and explicit.
Implicit Casting: Also known as widening or automatic type conversion, it occurs when the Target type has a larger range than the source type. For example:
int num = 10; long bigNum = num;
The value of num (source type) is automatically converted to a long (Target type).
Explicit Casting: Also known as narrowing or manual type conversion, it occurs when the Target type has a smaller range than the source type. For example:
double bigValue = 3.14; int smallValue = (int) bigValue;
The value of bigValue (source type) is explicitly casted to an int (Target type).
Data types in Java define the nature of variables and determine what kind of values they can hold. Understanding and using the appropriate data types is crucial for writing efficient and error-free Java programs.