Primitive variable in Java – Part 2

In our previous tutorial, we looked at five of the eight primitive data types in Java. Now let’s look at the 3 remaining primitive data types.

float

The float is a 32-bit data type that has a very large value. I do not list here because it is not necessary. Very long one 😀

The declaration for a float variable is as follows:

double

Double is larger than float, 64 bits, and its value is greater than float.

Example of declaring a double variable:



Some notes of float and double

– The default data type for decimals is double, and because floats cannot be automatically converted to double:

Primitive variable in Java - Part 2

so when declaring a variable as float, you must add the value of the variable F or f behind the variable.

Primitive variable in Java - Part 2

In addition, the double variable can also be added after the value of the letter D or d but redundant because it is already double by default.

– In Java 7, we can also use the underscore in the declaration of the value of the variable as the data types byte, short, int, long. The principle of use is the same as byte, short, int, long but with the following additional note:

  • You cannot put the underscore before D, d, F or f.
  • You cannot put the underscore next to the decimal point in the float, double declaration.

boolean

A boolean variable can store one of two values true or false.

For example, declare a boolean variable as follows:

Add Comment