JavaRush /Java Blog /Random EN /Primitive Java Types
articles
Level 15

Primitive Java Types

Published in the Random EN group
As already mentioned, the following primitive types are defined in Java: Primitive Java Types - 1
  • 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 - 2As 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 longby 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-9and letters A-For 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 bytehave a maximum value 0x7Fand a minimum value of - 0x80, and octal numbers have respectively 177and - 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 charand 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 0000to 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
Primitive Java Types - 3

2. Real types of variables

The Java language supports numbers and floating-point variables of regular and double digits - types floatand double. For floating point numbers, you must specify the integer and fractional parts, separated by a dot, for example, 4.6or 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 - 4Floating-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 floatto 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 intequal 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
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION