Java data types

A variable could be declare as one of them.

byte8-bit signed integerbyte <variable_name> = <value>;byte age = 32;
short16-bit signed integershort <variable_name> = <value>;short year = 2022;
int32-bit signed integerint <variable_name> = <value>;int quantity = 100;
long64-bit signed integerlong <variable_name> = <value>;long population = 7000000000L;
float32-bit floating pointfloat <variable_name> = <value>;float price = 49.99f;
double64-bit floating pointdouble <variable_name> = <value>;double interestRate = 0.05;
booleanTrue or falseboolean <variable_name> = <value>;boolean isValid = true;
char16-bit Unicode characterchar <variable_name> = <value>;char grade = 'A';
StringSequence of charactersString <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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top