Programming: Java. Data types

The data type is determined by three components:

  • a set of values ​​or objects;
  • a set of operations that can be applied to all values ​​in a set;
  • presentation of data determining their storage.

What are the data types in Java?

The programming language contains some predefined built-in types and allows programmers to define their own custom ones.

In Java, data types are divided into primitive and referential.

“Primitive” means that its further division is impossible. To expand or change its programming language does not allow. This data type is described by primitive and other user types.

A variable of primitive type contains a value, and a reference one contains the address of the object in memory.

java data types

Java language. Data Types: Time and Date

Compound data is divided into classes, interfaces, and arrays. The members of the interface type are abstract methods and constants. In Java, date and time data types are specified by the Date () constructor:

  • d = new Date ().

An example of a link is also a string.

Java language. Data Types: String

A string is a class defined in the Java library, and it can be used to work with text (a sequence of characters).

The declaration of the referenced String variable is as follows: String str.

Before you assign an object reference to such a variable, you must create it using the new operator. For example, you can create an object of class String with the text "Hello":

  • str = new String ("Hello").

What happens when this code is executed? First, memory is allocated, and the name str is associated with this memory cell. This is no different from declaring a primitive variable. The second code fragment creates a String object in memory with the text “Hello” and stores a link to it (or a memory address) in str.

Java reference data types also allow you to assign a reference to an object stored in one variable to another. Both of them refer to the same object in memory. This can be achieved as follows:

  • String str1;
  • String str2;
  • str1 = new String ("Hello");
  • str2 = str1;

There is a null permalink that can be assigned to any reference variable. It does not refer to any object.

A String object is created using the new operator. But since strings are often used, there is an easier way to create one. All string literals, i.e. a sequence of characters enclosed in double quotes, are treated as String objects. Therefore, instead of the new operator, string literals can be used:

  • String str1 = "Hello".

Primitive Java data types are byte, short, int, long, char, float, double and boolean. They are divided into two categories: logical and numerical. The latter can be divided into integers and floating point numbers.

Java integer data types are numeric types whose values ​​are integers. There are five of them: byte, short, int, long and char.

java data types

Int

Int is a 32-bit signed primitive data type. The variable takes 32 bits of memory. The valid range is from -2147483648 to 2147483647 (-2 31 to 2 31 - 1). All integers in this range are integer literals or constants. For example, 10, -200, 0, 30, 19 are int literals. They can be assigned to an int variable:

  • int num1 = 21;

Entire literals can be expressed as binary, octal, decimal, and hexadecimal numbers.

When a literal starts from zero and has at least two digits, it is considered written in octal format. 0 and 00 represent the same value - zero.

All int literals in hexadecimal format begin with 0x or 0x, and they must contain at least one hexadecimal digit:

  • int num1 = 0x123.

Binary- format int literals begin with 0b or 0B:

  • int num1 = 0b10101.

java data types time

Long

This is a 64-bit signed primitive type. Used when the result of calculations may exceed the range of int. The long range is from -2 63 to 2 63 - 1. All integers in this range are long type literals.

To distinguish int and long data types in Java, a literal of the last type always ends with L or l.

Long integer literals can also be expressed in octal, hexadecimal, and binary formats.

When a long literal is assigned to a long variable, the Java compiler checks the assigned value and makes sure that it is in an acceptable range; otherwise, a compilation error will occur.

Since the range of int is less than that of long, the value of an int variable can always be assigned to a variable of type long. But reverse assignment is not possible even within the range of int. An explicit reference is used for this:

  • num1 = (int) num2;

java string data types

Byte

Byte is an 8-bit integer primitive type. Its range is from -128 to 127 (-2 7 to 2 7 - 1). This is the smallest integer type available in Java. As a rule, byte variables are used when a program involves a lot of values ​​in the range from -128 to 127, or when working with binary data. Unlike int and long literals, byte literals are missing. However, you can assign a byte variable to any int literal, since it overrides the byte range.

If the value of the variable is out of range, Java will throw a compiler error.

In addition, you can assign only an int literal, but not the value stored in the int variable, since this may cause loss of precision. This will require an explicit type cast.

  • b1 = (byte) num1.

Short

Represents a 16-bit signed integer primitive data type. Its range is from -32768 to 32767 (or -2 15 to 2 15 - 1).

As a rule, the need for short variables arises when the program uses a large number of values ​​that do not exceed the specified range. There is no short literal, but assignment of any int literal within the short range is possible. The value of a byte variable can always be assigned. The rest of the rules for assigning an int or long short variable are the same as for byte.

java primitive data types

Char

Char is a 16-bit unsigned primitive data type that represents a Unicode character. The absence of a sign means that the variable cannot have a negative value. The range is from 0 to 65535, which matches the Unicode character set encoding. A literal represents a char value and can be expressed in the following forms:

  • single quotation mark character;
  • sequence of control characters;
  • Unicode control character sequence
  • a sequence of octal control characters.

A character can be expressed by enclosing it in single quotes: char C1 = 'A'. Double quotation marks indicate a string literal that cannot be assigned to a char variable, even if the string consists of only one character. This is not permissible since a reference to a primitive variable is not assigned. All string literals are objects of the String class and, therefore, are references, while character literals are of a primitive type.

The literal expressed by the escape sequence is written as a backslash with the character in single quotes. There are 8 of them: '\ n', '\ r', '\ f', '\ b', '\ t', '\\', '\ ”', '\' '.

The Unicode control sequence has the form '\ uxxxx', where \ u (the backslash followed by the lowercase u) indicates its beginning, and xxxxx represents exactly four hexadecimal digits of the character code in the Unicode system. For example, 'A' has a value of 65 in decimal and 41 in hex. Thus, this character can be represented as '\ u0041'.

The octal escape sequence is written as '\ nnn', where n is the octal digit (0-7). The range of values ​​is from '\ 000' to '\ 377', where 377 8 corresponds to 255 10 . Therefore, it is used to represent characters with a code from 0 to 255, which is necessary for compatibility with other programming languages. Unlike a Unicode sequence where all four hexadecimal digits are needed, here you can use 1, 2 or 3 octal: '\ n', '\ nn' or '\ nnn'.

java reference data types

Boolean type

Boolean has only two valid values: true (true) and false (false). They are called Boolean literals. A logical variable cannot be cast to another type and vice versa. Java does not determine the size of boolean - it depends on the specific implementation of the Java virtual machine.

Java floating point data types

The number that contains the fractional part in the computer's memory can be stored in a representation with a fixed number of digits before and after the point or indicating its position. Since the number of digits can vary, they say that the point "floats".

In Java, floating point data types use 32 bits. According to the IEEE 754 standard, this corresponds to a single precision, which allows us to represent, for example, the numbers 1.4 x 10 -45 and 3.4 x 10 38 , both positive and negative.

All real numbers that end in f or F are called float literals. They can be presented in decimal format and in the form of scientific notation. For instance:

  • float f1 = 3.25F;
  • float f2 = 32.5E-1F;
  • float f3 = 0.325E + 1F.

The type defines two zeros: + 0.0F (or 0.0F) and -0.0F. However, for comparison purposes, both zeros are considered equal. In addition, he defined two types of infinity: positive and negative. The results of some operations (for example, dividing 0.0F by 0.0F) are not defined and are represented by the special value NaN.

java data types date and time

Double precision

Double uses 64 bits to store floating point numbers. The double-precision number can represent positive and negative values ​​of 4.9 x 10 -324 and 1.7 x 10 308 .

All real numbers are double literals by default. Optionally, they can be explicitly indicated by the suffix d or D, for example, 19.27d. A double literal can be expressed in decimal form and in scientific notation.

Source: https://habr.com/ru/post/K2652/


All Articles