A variable could be declare as one of them.
Name | Description | Syntax | Example |
---|---|---|---|
byte | 8-bit signed integer | byte <variable_name> = <value>; | byte age = 32; |
short | 16-bit signed integer | short <variable_name> = <value>; | short year = 2022; |
int | 32-bit signed integer | int <variable_name> = <value>; | int quantity = 100; |
long | 64-bit signed integer | long <variable_name> = <value>; | long population = 7000000000L; |
float | 32-bit floating point | float <variable_name> = <value>; | float price = 49.99f; |
double | 64-bit floating point | double <variable_name> = <value>; | double interestRate = 0.05; |
boolean | True or false | boolean <variable_name> = <value>; | boolean isValid = true; |
char | 16-bit Unicode character | char <variable_name> = <value>; | char grade = 'A'; |
String | Sequence of characters | String <variable_name> = <value>; | String name = "John Doe"; |
Note that when declaring variables of type float
, the f
suffix must be added to the value to indicate that it is a floating point value. When declaring variables of type long
, the L
suffix must be added to the value to indicate that it is a long integer value.
Also, String
is not a primitive data type in Java, but it is often used as if it were one. A String
is actually an object, but Java provides special support for String
objects, making it easy to use them in a way that is similar to primitive data types.