You get a bonus - 1 coin for daily activity. Now you have 1 coin

java data types

Lecture



Constants

In Java, you can write constants of different types in different forms. We list them.

Whole

Integer constants can be written in three number systems:

  • in decimal form: +5, -7, 12345678 ;
  • in octal form, starting from zero: 027, -0326, 0777 ; in the record of such constants numbers 8 and 9 are not allowed;

Comment

The number starting from zero is written in octal, not decimal.

  • in hexadecimal form, starting with a zero and the Latin letter x or X: 0xff0a, 0xFC2D, 0x45a8, 0X77FF ; here lowercase and uppercase letters do not differ.

Integer constants are stored in an int format (see below).

At the end of an integer constant, you can write a capital letter L or a lowercase l , then the constant will be stored in a long format like long (see below): + 25L, -0371, OxffL, OXDFDF1 .

Board

Do not use when writing long integer constants a lowercase Latin letter l , it is easy to confuse with the unit.

Valid

Valid constants are written only in decimal notation in two forms:

  • with a fixed point: 37.25, -128.678967, +27.035 ;
  • with floating point: 2.5e34, -0.345e-25, 37.2E + 4 ; You can write a lowercase or uppercase Latin letter E ; spaces and brackets are not allowed.

At the end of a real constant, you can put the letter F or f , then the constant will be saved in a format of type float (see below): 3.5f, -45.67F, 4.7e-5f . You can also add the letter D (or d ): 0.045D, -456.77889d , meaning double , but this is unnecessary, since real constants are already stored in double format.

Characters

The following forms are used to record single characters.

  • Printed characters can be written in apostrophes: ' a ', ' N ', ' ? '.
  • Control characters are written in backslash apostrophes:
    • ' \ n ' is the newline newline character with ASCII 10 code;
    • ' \ r ' - carriage return symbol CR with code 13;
    • ' \ f ' is the translation character of the FF page with code 12;
    • ' \ b ' is the return symbol for step BS with code 8;
    • ' \ t ' - the symbol of horizontal tabulation of NT with code 9;
    • ' \\ ' - backslash;
    • ' \ " ' - quotation mark;
    • ' \' 'Is an apostrophe.
  • The code of any character with a decimal encoding from 0 to 255 can be set by writing it with no more than three digits in the octal number system in apostrophes after the backslash: ' \ 123 ' is the letter S , ' \ 346 ' is the letter Ж in the encoding CP1251. It is not recommended to use this recording form for the printable and control characters listed in the previous paragraph, since the compiler will immediately convert the octal entry to the above form. The largest code ' \ 377 ' is a decimal number 255.
  • The code of any character in the Unicode encoding is typed in apostrophes after the backward slash and the Latin letter q with exactly four hexadecimal digits: ' \ u0053 ' is the letter S , ' \ u0416 ' is the letter Ж.

Characters are stored in a char format (see below).

Note

Unicode capitalized Russian letters occupy the range from ' \ u0410 ' - capital letter A , to ' \ u042F ' - capital letter I , lowercase letters from ' \ u0430 ' - a , to ' \ 044F ' - me .

In whatever form the characters are written, the compiler translates them into Unicode, including the source code of the program.

Comment

The compiler and the Java runtime system work only with Unicode encoding.

Strings

Character strings are quoted. Control characters and codes are written in lines in the same way, with a backslash, but, of course, without apostrophes, and have the same effect. Strings can be located only on one line of the source code, you cannot put the opening quote on one line, and the closing one - on the next.

Here are some examples:

"This is a string \ n with carry"

"\"Spartak is a champion!"

Comment

Character strings cannot be started on one line of source code, but terminated on another.

For string constants defined the operation of couplings, denoted by a plus.

" Clutch" + "lines" results in the string "Line clutch" .

To write a long string in the form of a single string constant, it is necessary after the closing quotation mark on the first and next lines to put a plus +; then the compiler will compile two (or more) lines into one string constant, for example:

"One string constant written" +

"on two lines of source text"

Anyone who tries to display characters in Unicode, for example, the word "Russia":

System.out.println ("\ u0429 \ u043e \ u0441 \ u0441 \ u0438 \ u044f");

should know that Windows 95/98 / ME does not work with Unicode at all, and Windows NT / 2000 uses the Terminal font in which the Russian letters are located in the initial Unicode codes, for some reason, in the CP866 encoding, and scattered across other unicode segments.

Not all Unicode fonts contain glyphs of all characters, so be careful when displaying strings in Unicode.

Board

Use Unicode directly only in extreme cases.

Names

The names of variables, classes, methods and other objects can be simple (the common name is idenifiers) and qualified names. Identifiers in Java are made up of so-called Java letters (Java letters) and Arabic numerals 0-9, and the first character of the identifier cannot be a digit. (Indeed, how to understand the record 2E3 : as the number 2000.0 or as the name of a variable?) The letters Java necessarily include uppercase and lowercase Latin letters, a dollar sign $ and an underscore _ , as well as characters of national alphabets.

Comment

Do not include a dollar sign in the names. The Java compiler uses it to write nested class names.

Here are examples of the correct identifiers:

a1 my_var var3_5 _var veryLongVarName

aName theName a2Vh36kBnMt456dX

In names, it is better not to use the lowercase letter l , which is easily confused with the unit, and the letter o, which is easy to take as zero.

Do not forget about the recommendations of the "Code Conventions".

In the Character class, which is part of the Java API, there are two methods that check whether a given character is suitable for use in an identifier: isJavaidentifierStarto , which checks whether a character is a Java letter, and isJavaldentifierPart () , which determines if a character is a letter or a digit.

Java service words such as class , void , static are reserved, and cannot be used as identifiers of their objects.

A qualified name is several identifiers separated by dots, without spaces, for example, the name System.out.println that we have already encountered .

Primitive data types and operations

All types of source data embedded in the Java language are divided into two groups: primitive types and reference types .

Reference types are divided into arrays , masses (classes) and interfaces (interfaces).

There are only eight primitive types. They can be divided into the boolean (sometimes say boolean) type and the numeric type.

Numeric types are integers (integral [The name "integral" is not an established term. This is the category of integer data types in The Java Language Specification, Second Edition. James Gosling, Bill Joy, Guy Steele, Gilad Bracha (see introduction). - Ed.]) And floating-point types.

There are five integer types: byte , short , int , long , char .

Symbols can be used wherever the int type is used, so JLS classifies them as integer types. For example, they can be used in arithmetic calculations, for example, you can write 2 + 'x' , the Unicode encoding '\ u04i6' of the letter 'x' will be added to the two. In decimal form, this number is 1046 and as a result of addition we get 1048.

Recall that in the record 2 + "F" plus is understood as the concatenation of strings, the two will be converted into a string, the result will be the string "2zh . "

There are two real types: float and double .

In fig. 1.2 shows the hierarchy of Java data types.

Since the name of a variable cannot determine its type, all variables must be described before using them. The description is that the name of the type is written, then, separated by a space, the list of variable names separated by commas. For all or some variables, you can specify initial values ​​after the equal sign, which can be any constant expressions of the same type. The description of each type is completed with a semicolon. The program can be as many descriptions of each type.

Specialist Note

Java is a strongly typed language.

Let us examine each type in more detail.

  java data types

Fig. 1.2. Java language data types

Boolean type

The boolean values ​​of the boolean type arise as a result of various comparisons, like 2> s, and are used mainly in conditional and loop operators. There are only two logical values: true (true) and false (false). These are Java service words. The description of variables of this type looks like this:

boolean b = true, bb = false, bool2;

Assignment operations can be performed on logical data, for example, bool2 = true , including those with logical operations; comparison for equality b == bb and for inequality b! = bb , as well as logical operations.

Logical operations

Logical operations:

  • negation (NOT) ! (indicated by an exclamation mark);
  • conjunction (AND) & (ampersand);
  • disjunction (OR) | (vertical bar);
  • exclusive OR (XOR) ^ (quads).

They are performed on logical data, their result is also the logical value true or false . About them you can not know anything, except what is presented in table. 1.1.

Table 1.1. Logical operations

b1
b2
! b1
b1 & b2
b1 | b2
b1 ^ b2
true
true
false
true
true
false
true
false
false
false
true
true
false
true
true
false
true
true
false
false
true
false
false
false

Words these rules can be expressed as:

  • negation changes the meaning of truth;
  • a conjunction is true only if both operands are true;
  • disjunction is false only if both operands are false;
  • exclusive OR is true only if the values ​​of the operands are different.

Comment

If Shakespeare were a programmer, he would have written the phrase “That be or not to be”: 2b | ! 2b.

In addition to the four logical operations listed above, there are two more abbreviated logical operations:

  • shortened conjunction (conditional-AND) && ;
  • shortened disjunction (conditional-OR) || .

Double ampersand and vertical bars should be written without spaces.

The right operand of reduced operations is calculated only if the result of the operation depends on it, that is, if the left operand of a conjunction is true , or the left operand of disjunction is false .

This rule is very convenient and cleverly used, for example, you can write expressions (n! = 0) && (m / n> 0.001) or (n == 0) || (m / n> 0.001) without fear of division by zero.

Comment

Practically always abbreviated logical operations are used in Java.

Integer types

The Java language specification, JLS, defines the bit depth (the number of bytes allocated to store type values ​​in RAM) and the range of values ​​of each type. For integer types, they are listed in Table. 1.2.

Table 1.2. Integer types

Type of
Digit (bytes)

Range

byte
one
from -128 to 127
short
2
from -32768 to 32767
int
four
from -2147483648 to 2147483647
long
eight
from -9223372036854775808 to 9223372036854775807
char
2
from '\ u0000' to '\ uFFFF' , in decimal form from 0 to 65535

However, for Java, bit depth is not so important, on some computers it may differ from the one indicated in the table, but the range of values ​​must be strictly maintained.

Although the type char takes two bytes, it participates in arithmetic calculations as the type int , it is allocated 4 bytes, the two most significant bytes are filled with zeros.

Examples of defining integer variable types:

byte b1 = 50, b2 = -99, bЗ;

short det = 0, ind = 1;

int i = -100, j = 100, k = 9999;

long big = 50, veryBig = 2147483648L;

char c1 = 'A', c2 = '?', newLine = '\ n';

Integer types are stored in binary form with additional code. The latter means that for negative numbers not their binary representation is stored, but the additional code of this binary representation.

The additional code is obtained as follows: in binary representation, all zeros change by one, and units by zeros, after which one is added to the result, of course, in binary arithmetic.

For example, the value 50 of the variable b1 , defined above, will be stored in one byte with the contents 00110010 , and the value -99 of the variable b2 - in the byte with the contents, which we calculate as follows: translate the number 99 into binary form, getting 01100011 , changing the ones and zeros, getting 10011100 , and adding one, finally getting a byte with the contents of 10011101 .

The meaning of all these difficulties is that adding a number to its additional code in binary arithmetic will result in a zero, the most significant bit is simply lost. This means that in such strange arithmetic the additional code of a number is the opposite number, a number with the opposite sign. And this, in turn, means that instead of subtracting number B from number A, one can add an additional code to number B to A. Thus, the subtraction operation is excluded from the set of machine operations.

On whole types it is possible to make a lot of operations. Their set goes back to language C, it turned out to be convenient and wanders from language to language almost unchanged. The features of using these operations in the Java language are shown in the examples.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Object oriented programming

Terms: Object oriented programming