# environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

# What is an expression

I think I'm used to reading the formulas by performing four arithmetic operations from a young age, but be aware that there are some differences between the calculator and the program, which are the same electronic calculator.

In Java, an "expression" consists of the following two.

1, ** How to calculate [Operator] ** ex) + (add),-(subtract), * (multiply), / (divide) etc ...

1. ** Target of operation Operand ** ex) 1 and 2 in 1 + 2

# Practice of formula

The same + can be used in programs for both "string concatenation" and "operation". The character string is the contents enclosed in "". The procedure for actually displaying is omitted.

#### `Connection mistake`

``````
//When you have the 5th lottery

"My lottery number is" + 2 + 3 + "It is."
//mistake. This result is"My lottery number is 13."Will be.
``````

This mistake should have given priority to the numbers being calculated before concatenating them as strings with +. 1 and 3 are recognized as operands and are displayed as characters in order.

Correct answer: "My lottery number is" + (2 + 3) + "."

** Operators have precedence **, and you may have heard that "multiplication and division have priority" and "the contents of parentheses have priority" in mathematics.

Correct example ex) (1 + 2) Result: 3

ex) "Java" + "Script" Result: JavaScript

By using variables as operands instead of numbers, you can express the calculation results as they appear, and you can create your own favorite sentences. The procedure of calculation is enclosed in {}.

ex) ｛ int mathExam = 60; int englishExam = 90; int worldHistoryExam = 65;

int myTotalScore = mathexam + englishExam + worldHistoryExam;

"My total score was" + myTotalScore + "."; ｝

Result: My total score was 215.

If you look at each as a block of operands, you can see what the actual numbers are. It is necessary to put the comma and punctuation mark in the character string in advance.

# Substitution

In the above example, the sum of the scores of the three subjects on the right side is assigned to the myTotalScore variable. This formula is confusing when one considers that the = operator "indicates that the right and left sides are equal". In the program, it is better to think of the = operator as ** "substitute the operation result (evaluation) on the right side into the variable on the left side" **.

I will explain by giving the formula of the loop counter (variable that counts the current lap several times, often used).

ex)　 int loopCounter = 0;{ ... loopCouter = loopCounter + 1; } The right side and the left side do not match, but it is programmatically correct. First, the right side is evaluated, and at the time of the first week, the value obtained by adding 1 to the initial value 0 is included in the loopCounter. If this mechanism occurs every time the loop goes around, the value of the loopCounter variable will increase by 1.

# At the end

I'm exhausted just by thinking about expressions and assignments, so I'll post "operator types", "assignment operators", "operation priorities", etc. tomorrow.

# reference

I haven't written it before, so I'll give you my reference book. I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition