As already mentioned, the following primitive types are defined in Java:
- integer types;
- real types;
- boolean type.
A primitive variable declaration statement can be followed by an initialization statement "
=
", which assigns an initial value to the created variable.
1. Integer variable types
Integer types differ in the amount of memory allocated to them. Characteristics of integer types are given in Table. 1.1.
Tab. 1.1. Characteristics of Java Integer Types
![Primitive Java Types - 2]()
As you can see from the table above, integer variables, with the exception of type
char
, are considered signed variables in the Java language. Integer constants can be specified in a program in one of three ways: as decimal, hexadecimal, and octal values. By default, all numbers are interpreted as
decimal and are of type
int
. You can explicitly indicate which type you belong to
long
by adding a letter
"l"
or a letter to the end of the number
"L"
.
A hexadecimal value is specified using the characters
"0x"
or
"0X"
, followed by a number value (numbers
0-9
and letters
A-F
or
a-f
), for example:
0x7FFF
. A number in octal notation must begin with a zero followed by one or more octal digits, such as
077777
. Octal and hexadecimal numbers can be both positive and negative and vary in the same ranges as numbers in decimal representation (for example, hexadecimal numbers of the type
byte
have a maximum value
0x7F
and a minimum value of -
0x80
, and octal numbers have respectively
177
and -
200
)
Examples of declaring integer variables :
int x = 0;
long i, j, k;
byte a1 = 0xF1, a2 = 0x07;
short r1 = 017;
Characters in Java are defined with a keyword
char
and are implemented using the Unicode standard. You can specify a constant character in the program or as a regular character. The character value must be enclosed in a pair of single apostrophes, for example:
char symbol='f';
Another way to write characters: a character pair
"\u"
followed by a four-digit hexadecimal number (in the range from
0000
to
FFFF
) representing the Unicode character code, like this:
char symbol = '\u0042';
Some characters that are not available on the keyboard can be specified using so-called escape sequences containing the character "
\
" followed by a literal character that identifies the escape sequence, as shown in Table 1. 1.2.
Tab. 1.2. Escape sequences used in the Java language
2. Real types of variables
The Java language supports numbers and
floating-point variables of regular and double digits - types
float
and
double
. For floating point numbers, you must specify the integer and fractional parts, separated by a dot, for example,
4.6
or
7.0
. For large numbers, you can use the exponential notation (the symbol
"e"
or symbol is used to separate the mantissa from the exponent
"E"
), for example, the number -3.58×107 is written as
–3.58E7
, and the number 73.675×10-15 as
73.675e-15
. Characteristics of real Java types are presented in Table. 2.1.
Tab. 2.1. Characteristics of Java real types
![Primitive Java Types - 4]()
Floating-point variables can store not only numerical values, but also any of the specially defined flags (states): negative infinity, negative zero, positive infinity, positive zero, and not-a-number,
NaN
. All floating point constants are assumed to be of type
double
. To specify a number of type , you must append a symbol or symbol
float
to the end of it .
Examples of declaring floating point variables:"f"
"F"
float x1 = 3.5f, x2 = 3.7E6f, x3 = -1.8E-7f;
double z = 1.0;
3. Boolean type of variables
Boolean type variables (logical variables) can take one of two values: "true" or "false" and are used in programming languages in relational (comparison) and logical operations. So, the result of the comparison
5 > 3
will be "true", and the result of the comparison
8 < 1
will be false. Unlike C, where the false result is associated with an integer value of type
int
equal to 0, and the true result is a non-zero value of type
int
, and, accordingly, an integer value (usually 0 or 1) is assigned to the results of the comparison, in Java for boolean variables its own separate data type.
Boolean type variables in Java are defined using the boolean keyword and can only have one of two values:
true or
false , for example
boolean switch = true;
Link to original source:
Java Primitive Types
GO TO FULL VERSION