A value can be assigned to a person
In programs you need a way to save values, e.g. intermediate results of calculations. This is done with the help of variables. Just like in math, a variable is a placeholder for a value.
Declaration of variables
The term placeholder is to be taken literally here. When you declare a variable, the computer provides a place in memory for the value. So that the computer knows how to interpret the content of the memory space, the type of the value is specified when it is declared.
A type is a range of values, i.e. a set of possible values. One example is the type int (English: integrity - integer). The possible values of the type int are the whole numbers in the range -2147483648, ..., 2147483647.
Associated with a type are the permissible operations that can be applied to values of this type, e.g. an addition for the type int.1)
Example: The following statement declares two variables a and b that are intended to accept values of the type int.
A variable that is intended to hold a value of the type int is briefly referred to as an int variable or a variable of the type int.
Assignment of values to variables
With a value assignment, a variable is assigned an actual value.
Example: The following statements are used to first declare two variables a and b of type int. Then the value 17 is assigned to the variable a. The value 21 is then assigned to the variable b.
The computer assigns a value by first evaluating the expression to the right of the "=" value assignment sign. He assigns the determined value to the variable on the left-hand side of the value assignment symbol.
So in the second assignment of values in this example, the computer evaluates the expression a + 4. To do this, he looks in the memory to see what value a has (17) and adds 4 to this value. He assigns the result 21 to the variable b. The variable b then has the value 21.
The declaration of a variable and the assignment of an initial value can also be carried out together.
Example: The instructions from the previous example can also be written like this:
There must always be a variable on the left side of the value assignment sign. There must always be an expression on the right-hand side. An expression is a term that is evaluated by the computer and yields a value of a certain type. Possible value types in Java include numbers (type int or double), strings of characters (type string) and truth values (type boolean - named after G. Boole).
The simplest expression is the direct specification of a value, i.e. a number, a series of characters or a truth value.
Example: In the following section of the program, the variables x, s, and w are each assigned the numerical value 2, the character string "Hello" and the truth value true.
|int x; String s; boolean w; x = 2; s = "Hello"; w = true;|
The second simplest expression is a single variable. The computer evaluates such an expression by reading the value of the variable from memory.
Example: The value of x is assigned to the variable y, i.e. the value 2:
Complex expressions are created by linking values and variables with arithmetic symbols and possibly by putting them in brackets.
Example: In the following program, three expressions occur, each of which results in a number, a character string and a logical value.
|int x = 2, y; String s; boolean w; y = 5 + (3 * x + 2) * x; s = "U" + x; w = x> 0;|
The variable y receives the value 21, the variable s receives the character string "U2" as its value and the variable w receives the truth value true.
When evaluating numerical expressions, the usual arithmetic rules apply - brackets are evaluated first, point calculation takes priority over line calculation, otherwise it is evaluated from left to right.
In addition to adding numbers, the + sign is also used to concatenate strings of characters. If a number and a string of characters are linked with +, the number is converted into a string of characters and concatenated with the string of characters.
The expression x> 0 yields a truth value, i.e. either true (true) or false (false), depending on whether the condition x> 0 is met or not.
Finally, calling a function that returns a value is an expression.
Example: The maximum of a and -a is assigned to the variable b:
The prerequisite is that the function max is defined and that a value has been assigned to the variable a beforehand.
When assigning a value, the type of the value must match the type of the variable. There are exceptions in the area of numeric types. For example, it is possible to assign a value of the type int (integer) to a variable of the type double (decimal number). The integer is automatically converted into a decimal point. For example, a 3 is converted to 3.0.
In the opposite direction, there is no automatic type adaptation. A value of the type double cannot be assigned to a variable of the type int unless the double value is explicitly converted beforehand. The explicit type conversion is known as casting to cast - to water). The value is poured into a new form, so to speak.
Example: With the statement a = (int) x; the double value 3.85 is trimmed so that it can be accepted by an int variable. The decimal places are simply cut off. Thus the variable a receives the value 3.
|int a; double x = 3.85; a = (int) x;|
The desired new type is placed in brackets in front of the expression to be converted; the type conversion only relates to the immediately following partial expression (priority like a sign). If this is to be deviated from, additional brackets must be used:
|int a, b; double x = 3.85; a = (int) x * 2; // a = 6 b = (int) (x * 2); // b = 7|
1) Attention: The addition with the type int is an addition modulo 232, i.e. unexpected results occur if the range of values is exceeded.
- Bitcoin Millionaire Club is a scam
- A 10 rupee coin is prohibited
- Can people be compelled to be virtuous?
- What is white lung disease
- Why is fructose pre-rotatory
- How painful can you feel
- Are limits and rules the same
- The HIV virus can move through the skin
- Someone is selling used socks
- Can you recycle paper stickers?
- Jesus' teachings were directly influenced by Buddhism
- Which bridge design is the cheapest
- Are McDonald's fries really the best
- What is brain dead
- How many divisions occur in meiosis
- What are some examples of therapeutic skills
- Twitter tried to buy GitHub
- How do I sell a page
- What does daana mean
- How much energy drives Iron Mans
- Have you read the texts from Slipknot?
- How much does it cost to leave Bangkok
- How to Recover Recently Deleted Photos
- What are good immigration advisors in Ahmadabad