what is data type?
A data type in Java defines the kind of data a variable can hold, specifying the size and type of values that can be stored. This ensures that operations performed on the data are type-safe and efficient.
Data types are divided into two groups:
Primitive data types - includes byte, short, int, long, float, double, boolean and char
Non-primitive data types - such as String, Arrays and Classes (you will learn more about these in a later chapter)
A primitive data type specifies the type of a variable and the kind of values it can hold.
There are eight primitive data types in Java:
Data Type Description
byte : Stores whole numbers from -128 to 127
short : Stores whole numbers from -32,768 to 32,767
int : Stores whole numbers from -2,147,483,648 to 2,147,483,647
long : Stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float : Stores fractional numbers. Sufficient for storing 6 to 7
decimal digits
double : Stores fractional numbers. Sufficient for storing 15 to 16
decimal digits
boolean : Stores true or false values
char : Stores a single character/letter or ASCII values
Difference between double and float Java types
The key difference between a float and double in Java is that a double can represent much larger numbers than a float. Both data types represent numbers with decimals, but a float is 32 bits in size while a double is 64 bits. A double is twice the size of a float — thus the term double.
Declaring a float:
Use the float keyword.
Append an f or F to the value to indicate it's a float.
float myFloat = 3.14f; // The 'f' suffix indicates it's a float
Declaring a double:
Use the double keyword.
No suffix is required for double literals, but you can optionally use d or D.
double myDouble = 3.14159; // No suffix needed for double
The default values for the primitive data types in Java are as follows:
byte: 0
short: 0
int: 0
long: 0L
float: 0.0f
double: 0.0d
char: '\u0000' (null character)
boolean: false
Top comments (0)