/ / Programming: Java. Data types

Programming: Java. Data types

The data type is defined by three components:

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

What are the types of data 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 reference types.

"Primitive" means that its furtherdivision is impossible. Expand or change its programming language does not allow. This type of data is described by primitive and other user-defined types.

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

java data types

The Java language. Data types: time and date

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

  • d = new Date ().

An example of a link is also a string.

The 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 reference String variable is as follows: String str.

Before assigning a reference to an object of such a variable, you must create it with the new operator. For example, you can create an object of the String class 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 with the text "Hi" in memory and stores a reference to it (or memory address) in str.

The reference data types of Java also allowassign a reference to an object stored in one variable, another. Both 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 constant null reference, which can be assigned to any reference variable. It does not refer to any object.

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

  • String str1 = "Hello".

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

The entire Java 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 allowable range is from -2147483648 to 2147483647 (-231 up to 231 - 1). All integers in this range are integer literals or constants. For example, 10, -200, 0, 30, 19 are literals int. They can be assigned to int:

  • int num1 = 21;

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

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

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

  • int num1 = 0x123.

Int-literals in binary format 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 the calculation can exceed the range of int. Range long - from -263 up to 263- 1. All integers in this range are long-type literals.

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

Entire long-type literals can also be expressed in octal, hexadecimal, and binary formats.

When a long literal is assigned to a variable long,The Java compiler checks the value assigned and verifies that it is in an acceptable range; otherwise, a compilation error occurs.

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 int range. For this, an explicit indication is used:

  • num1 = (int) num2;

java data types string

Byte

Byte is an 8-bit integer primitive type. Its range is from -128 to 127 (-27 up to 27 - 1). This is the smallest integer type available in Java.Usually, byte variables are used when the program uses many values ​​in the range from -128 to 127, or when working with binary data. Unlike int and long literals, there are no byte literals. However, you can assign any int-literal to the byte variable, since it overrides the byte range.

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

In addition, you can only assign an int-literal, but not a value stored in the variable int, since it may lose precision. This requires explicit casting.

  • b1 = (byte) num1.

Short

It is a 16-bit signed whole primitive data type. Its range is from -32768 to 32767 (or -215 up to 215 - 1).

As a rule, the need for short variablesoccurs when the program uses a large number of values ​​that do not exceed the specified range. A short literal is missing, but it is possible to assign any literal int within the range short. The value of a byte variable can always be assigned. The remaining rules for assigning an int or long short-variable are the same as for byte.

primitive data types java

Char

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

  • a character enclosed in single quotes;
  • a sequence of control characters;
  • sequence of Unicode control characters;
  • a sequence of octal control characters.

The symbol can be expressed by concluding it insingle quotes: char C1 = "A". Double quotes denote a string literal that can not be assigned to a char variable, even if the string consists of only one character. This is unacceptable, since a reference to a primitive variable is not assigned. All string literals are objects of the String class and, therefore, references, whereas the character literals refer to the primitive type.

The literal, expressed by a control sequence, is written in the form of a backslash with a single-quoted character. There are 8 in total: '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 xxxx represents exactly four hexadecimal digits of the character code in the Unicode system. For example, "A" has a value of 65 in the decimal system and 41 in hexadecimal. Thus, this symbol can be represented as "u0041".

The octal control sequence is written as "nnn", where n is the octal number (0-7). The range of values ​​is from "8 corresponds to 25510. Therefore, it is used to representcharacters with code from 0 to 255, which is necessary for compatibility with other programming languages. Unlike a Unicode sequence where all four hex digits are required, you can use 1, 2, or 3 octal numbers: "n", "nn" or "nnn".

java reference data types

Boolean type

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

Java Floating Point Types

The number that contains the fractional part in memorycomputer can be stored in a view with a fixed number of digits before and after the point or indicating its position. Since the number of digits can vary, it is said that the point "floats".

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

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

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

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

java data types date and time

Double precision

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

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

Liked:
0
Popular Posts
Spiritual development
Food
yup