Postscript: 2020/3/4 Added reference books
This article is a memorandum. Although it is a reference book level content, the code posted in this article is ** Wrong things ** are the center. 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.
Language: Java11, JDK13.0.2 Operating environment: Windows 10
A variable is a mechanism for storing necessary values using the memory of a computer. It can be likened to a ** "box for storage" **.
Mandatory rules for handling variables ** 1, decide the [name] of the variable ** ** 2, after specifying the [type] of the variable ** ** 3, use after [declaring] the variable **
The combination of letters and numbers that can be used to name variables is called an ** Identifier **. However, there are some names that cannot be identifiers due to Java rules.
Below are the wrong examples and why.
Wrong_Identifier.java
1forAll //Don't start with numbers.
class //So-called "reserved words" that java has reserved for other purposes
all-for^one //Special characters cannot be used.
Here are some examples of names that can be identifiers.
one4all // It's OK because the head is an alphabet. classOfOurs // It's OK if it becomes a word other than a reserved word without inserting a space. all_for_one // Those connected by an underscore are OK.
A "type" is also called a "data type", and a named variable must have a predetermined value that can be stored in it. The basic Java types will be described later. Here, the contents when the substitution is made by mistake are described.
Wrong_DataType.java
char myCatName = "Tet" //This is a string, char is a single character or only a number
byte characterLevel = 128 //byte is-Can store from 128 to 127
The type determines what can be stored, and at the same time, the type determines how much computer memory is used. In the above example, char is 2 bytes and byte is 1 byte. The largest is 8 bytes of double.
In Java, variables cannot be used unless they are declared. The following is written in ** Initialization **, which stores the value in the variable at the same time as the variable is declared.
mistakeOfDeclaration.java
int moneyInMyPocket = 200;
int fleshJuice = 120;
moneyInMyHand = 80; //The type of the moneyInMyHand variable is not declared
About the last sentence int moneyInMyHand =moneyInMyPocket - fleshJuice The correct answer is to declare it properly as an int type and write it. The remaining two once declared do not need to be declared again unless the type changes.
Variables can be assigned to other variables as they are. However, both the type to be assigned and the type to be assigned must be the same. It can be inserted by converting the type, but it is omitted here.
Wrong_Assignment.java
int oneMagicNumber = 33-4
int similarMagicNumber = 33.4
Although similarMagicNumber is declared int, it is assigned a real number including a decimal point instead of an integer. 29, which is the result of the expression, is stored in oneMagicNumber.
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.
Recommended Posts