What does this mean in Java 1

1. Basic data types and their operators

double, int, boolean

byte, short, long, float, char

In mathematics you know natural, whole, rational, real and complex numbers. In Java, these variables correspond to so-called basic data types (also called primitive or simple types), the whole () and real () Numbers correspond. Each variable of such a data type takes up a fixed-size memory space when it is allocated (initialized), which is stored in byte is measured. Thereby it can only finite many Give numbers of the respective type.

4 bytes (32 bits) are used for a number. Therefore there are exactly 2 of these32 Numbers, namely the numbers -231 up to 231-1.

8 bytes are used for a number. These are represented internally as floating point binary numbers with 11 bits for the exponent, 1 bit for the sign and the remaining 52 bits for the mantissa. There are 2 in total64- Numbers that are unevenly distributed on the number line. In numerical mathematics, you will learn the concept of - and numbers associated with Floating point numbers and related Rounding errors.

Non-primitive data types are strings, fields and all classes. More on this later.

1.1 double

Keywords, identifiers, blocks, comments, statements, declaration of variables, value assignments

Arithmetic operators

We start with an example program, better with an example class (the digits in front of each line are for orientation only; they do not belong to the program), which does nothing other than add two numbers.

1 public class Ex1 2 { 3 public static voidmain(String [] args ) 4 { 5 double x, y, z; 6 x = 3.1675; y = -2.4567; 7 z = x + y; 8th System.out.println ( z ); 9 } // End main () 10 } // End of class

The Java-specific keywords ("terminal symbols") are all fat highlighted, the particularly important ones are highlighted in red. In the following explanation, the terms that explain the grammar ("non-terminal symbols") are green.

The program consists of a single class with an identifier: It can be executed using the method in program line 3, which is not yet explained here. One also speaks of an application class. This program has nothing to do with OOP. Nevertheless, like the following examples, it helps to familiarize yourself with the most important language elements. Later we will get to know classes, of which "instances" are formed in the form of objects.

One should be repeated


A distinction is made between upper and lower case. Failure to comply will result in cryptic error messages. In particular, the name of the file that contains the source program of a class (here) must match the identifier of the class (i.e. called here).

To structure the class, pay attention to (nested) blocks that are enclosed by curly brackets {and} and contain instructions. The largest block is the one that encompasses the class. Then the sub-block comes to the method. Comments (in one line) are introduced by two slashes ().

Line 5 contains declarations of the variables by name (identifier) ​​and type. In line 6 there are two value assignments, in line 7 a value is assigned with the help of an arithmetic operator + (addition). The equals sign used here can also be called an assignment operator, with a variable on the left and an expression on the right. System.out.println () in line 8 is a method provided by Java that prints what is inside the round brackets on the screen. Later we will see that character strings (keyword) are always displayed on the screen. Internally, the number is converted into a character string.

Basically, every variable has to be declared in a program: their name (identifier) ​​and their type are determined by this. The declaration can also be made directly with a Value assignment get connected:

5 doublex = 3.1675, y = -2.4567, z = x + y;

replaces the old lines 5-7.

Each instruction is always followed by a semicolon ; completed. Commas are used for listing as in line 5, which has just been modified.

Further arithmetic operators (Subtraction, multiplication and division) are -, *, /The power operator is provided in Java by the pow () method of the Math class:

z = Math.pow (x, y);

In line 8, the output can be made more user-friendly:

8 System.out.println ("Result: z =" + z);

Since the number z is internally converted into a character string, character strings are "added" (concatenated) here, so the operator + is not just an arithmetic operator.

Numbers can also be represented in floating point form: So is equivalent to. It stands for 10 -6.

Numbers are calculated with an accuracy of about 15 digits. If you are satisfied with a lower accuracy due to lack of storage space, you can use numbers.

1.2 int

Increment and decrement operators, short operators, arithmetic operators

The data type int represents whole numbers. For an int number, 4 bytes are used for storage; they make all integers from -231 up to 231-1 shown. If you cannot get by with this range of numbers, you can use the basic data type long; smaller number ranges are covered by the basic data types.

The arithmetic operators can of course also be used on numbers. You only have to be careful with the division. So results

int i = 7, j = 3, k = i / j;

the value (the largest integer that is less than or equal to the quotient).



int i = 7, j = 3; double x = i / j;

x receives the value 2. First

int i = 7, j = 3; double x = (double) i / j;

does what you want. The addition of a data type in round brackets is called as in casting, which converts one data type into another.

The binary should also be mentioned Modulo operator%, which delivers the integer remainder when dividing by in the case of numbers.

Short operators

Often you want to increase or decrease a whole number by one (e.g. in loops). This is done through the Short operators ++, -. E.g.

int i = 7, j = 3; i++; j--; System.out.println ("i =" + i + "j =" + j);

the values ​​and. Equivalent but a little longer

int i = 7, j = 3; i = i + 1; j = j-1;

or also (by using further short operators + =, - =)

int i = 7, j = 3; i += 1; j -= 1;

The Short operators ++, - can be put in front of or after a variable. The difference only has an effect if expressions of the form or are used. You can find more information in textbooks.

1.3 boolean

Comparison operators (==,! =, <,>, <=,> =) And logical operators (&, &&, |, ||, ^,!)

Boolean variables are logical variables and only have the values ​​(literals) true, false. Boolean expressions are mostly created by comparing numbers as in

boolean w = (9 == Math.pow (3,2));

You are always in round brackets. Note that two Equal signs can be used to compare two numbers, i.e. == is a (two-digit) comparison operator (relational operator). The result of both the comparison and the logical operators is always of the type. While the arguments for the comparison operators are numbers, for the logical operators they are also logical variables.

The (one-digit) logical operator! denied. Therefore! = Tests for inequality.

int x = 3; boolean w =! ((8

gives for the value.

Continue with bows.