Java for beginners, expressions and operators 1

Postscript: 2020/3/4 Added reference books

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

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

Recommended Posts

Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
Learn for the first time java # 3 expressions and operators
Classes and instances Java for beginners
[For beginners] Difference between Java and Kotlin
Kantai Collection Java # 1 Classes and Objects [For Beginners]
[For beginners] About lambda expressions and Stream API
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Java for beginners, data hiding
Java application for beginners: stream
Java while and for statements
List of frequently used Java instructions (for beginners and beginners)
[For beginners] Explanation of classes, instances, and statics in Java
AWS SDK for Java 1.11.x and 2.x
[For beginners] Run Selenium in Java
[For Java beginners] About exception handling
Nowadays Java lambda expressions and Stream API
Learn Java with "So What" [For beginners]
[Java] for Each and sorted in Lambda
Java starting from beginners, logical operators / conditional operators
[Java] Proxy for logging SQL and SQL results
[Java] Calculation mechanism, operators and type conversion
For JAVA learning (2018-03-16-01)
2017 IDE for Java
Java and JavaScript
XXE and Java
Java for statement
Review notes for Java 1.7 and later file copies
I studied for 3 weeks and passed Java Bronze
A collection of simple questions for Java beginners
[Introduction to Java] Basics of java arithmetic (for beginners)
Let's use Java New FileIO! (Introduction, for beginners)
[Java] for statement, while statement
Getters and setters (Java)
About products using crud processing and devise (for beginners)
[Java] Thread and Runnable
Sample (Java) for OAuth 2.0 authentication and access token acquisition
[For beginners] DI ~ The basics of DI and DI in Spring ~
[Java] Package for management
[For beginners] Quickly understand the basics of Java 8 Lambda
[Java] Sort the list using streams and lambda expressions
Java true and false
[Java] for statement / extended for statement
[Java] String comparison and && and ||
[For beginners] How to operate Stream API after Java 8
VSCode Java Debugger for Java Build failed Causes and countermeasures
Understand Java 8 lambda expressions
Comparison operators and conditionals
Countermeasures for Java OutOfMemoryError
[For beginners] Minimum sample to display RecyclerView in Java
Java development for beginners to start from 1-Vol.1-eclipse setup
NLP for Java (NLP4J) (2)
Java --Serialization and Deserialization
[Java] Arguments and parameters
(Memo) Java for statement
NLP for Java (NLP4J) (1)
Introduction to Java for beginners Basic knowledge of Java language ①
Array variables and associative arrays compiled by beginners for beginners
This and that for editing ini in Java. : inieditor-java
About Java lambda expressions