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 vary in the amount of memory allocated to them. Characteristics of integer types are given in table. 1.1.
Table 1.1. Characteristics of Java Integer Types
As you can see from the table below, 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, or octal values. By default, all numbers are interpreted as
decimal and are of type
int
. You can explicitly indicate type membership by adding a letter or letter
long
to the end of the number .
A hexadecimal value is specified using the characters or , followed by the value of the number (numbers and letters or ), for example: . A number in octal notation must begin with a zero, followed by one or more octal digits, for example . 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 have a maximum value and a minimum value of - , and octal numbers have, respectively, and - )
Examples of declaring integer variables :"l"
"L"
"0x"
"0X"
0-9
A-F
a-f
0x7FFF
077777
byte
0x7F
0x80
177
200
int x = 0;
long i, j, k;
byte a1 = 0xF1, a2 = 0x07;
short r1 = 017;
Characters in Java are defined using a keyword
char
and are implemented using the Unicode standard. You can specify a symbol constant in a program or as a regular symbol. The symbolic meaning must be enclosed in a pair of single apostrophes, for example:
char symbol='f';
Another way to write characters is a pair of characters
"\u"
followed by a four-digit hexadecimal number (ranging from
0000
to
FFFF
) representing the character's Unicode code, for example:
char symbol = '\u0042';
Some characters not found on the keyboard can be specified using so-called escape sequences,
\
which contain the character " " followed by an alphabetic character identifying the escape sequence, as shown in Table 1. 1.2.
Table 1.2. Escape sequences used in the Java language
2. Real types of variables
The Java language supports regular and double-bit floating-point numbers and variables - types
float
and
double
. For floating point numbers, you need to specify the integer and fractional parts, separated by a dot, for example
4.6
or
7.0
. For large numbers, you can use exponential notation (using a symbol
"e"
or symbol 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 is written as
73.675e-15
. The characteristics of Java real types are presented in Table. 2.1.
Table 2.1. Characteristics of Java Real Types
Floating-point variables can store not only numeric values, but also any of 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 floating point variable declarations:"f"
"F"
float x1 = 3.5f, x2 = 3.7E6f, x3 = -1.8E-7f;
double z = 1.0;
3. Boolean variable type
Boolean 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 comparison
5 > 3
will be “true”, and the result of the comparison
8 < 1
will be "false". Unlike C, where the result "false" is associated with an integer value of type
int
0, and the result "true" is associated with a non-zero value of type
int
, and, accordingly, the results of the comparison are assigned an integer value (usually 0 or 1), Java introduced Boolean variables its own, separate data type.
Boolean 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 source:
Java Primitive Types