I would like to take a quick look back at what I have been studying Java since the last time.
Java is an image of building a system while making boxes one by one.
Variables are the smallest unit in the box. For variables, prepare a box to store data, Putting in and out of data in it can do.
As an example
public class Main {
public static void main(String[] args) {
int age;
age = 20;
System.out.println(age);
}
}
First, create a box (variable declaration) to store data. A box does not mean anything. In Java you have to explicitly specify what the box is for Don't.
The "age" mentioned earlier is a box for storing an integer called "int type". Only integers (character strings and decimal points) can be entered. In Java, it is necessary to decide in advance the type of data to be inserted (* hereinafter referred to as "data type"). This is called a "variable declaration".
int age;
The above is a variable declaration that "creates a box called int type age".
int age;
age = 20;
Substitute the integer "20" in the created variable "age". As mentioned earlier, characters and decimal points cannot be entered in this int type variable "age". When I try to put it in, it cannot be compiled (converted to a computer-readable sentence) and I am asked to correct it.
int age = 20;
After data substitution
System.out.println(age);
System.out.println (); is a process to display the inside of () at the end on the console (where you talk to the computer). Since "age" is specified here, the "20" that was assigned earlier is displayed. So what happens if we substitute "30" for the variable "age" that contains "20"?
public class Main {
public static void main(String[] args) {
int age;
age = 20;
age = 30;
System.out.println(age);
}
}
Execution result As the name "number" changes, it will be overwritten with the most recently assigned one.
The data type is not limited to int type. There is also a data type for entering the "character string" and "decimal point number" mentioned earlier. I would like to summarize below.
Classification | Model name | Data to store |
---|---|---|
integer | long | Large integer |
int | Ordinary integer | |
short | Small integer | |
byte | Smaller integer | |
Decimal point | double | Double precision floating point number |
float | Single precision floating point numbers, numbers with a decimal point that can be a little vague | |
Boolean value | boolean | true or false |
letter | char | One character |
String | String | Character sequence |
At the beginning of study When using an integer int When using a decimal point double When using the alternative boolean When using a string String It's okay if you remember!
There are two types of data types: primitive types and reference types. Primitive types put values directly in the "box". Integer (long, int, short, byte) Decimal point number (double, float) Boolean value (boolean) Character (char) Reference types, on the other hand, assign addresses in memory rather than specific data. String type and I would like to summarize it later, but "array", "List", etc. are also included in this reference type. Instead of entering the "character string" directly, ... Specifically, one cushion is already included, but as shown in the figure, the first address of the "character" sequence of the name is substituted.
The use of variables has the following two advantages. ・ High versatility. ・ Easy to change and modify.
If you compare it to the sales of apples
public class Main {
public static void main(String[] args) {
//The price of an apple is 100 yen
int apple = 100;
//Sales in each region
int tokyo = 100 * apple;
int kanagawa = 80 * apple;
int tiba = 70 * apple;
//Total sales
int totalSales = tokyo + kanagawa + tiba;
//Display of total sales
System.out.println(totalSales);
}
}
Execution result The total sales are displayed. Here, if the price of apples is wrong, or if you want to get a quote when you raise the price, etc. There are various situations, but if you want to change the price of apples, you only need to make one correction.
public class Main {
public static void main(String[] args) {
//Adjust the price of apples from 100 yen to 120 yen
int apple = 120;
//Sales in each region
int tokyo = 100 * apple;
int kanagawa = 80 * apple;
int tiba = 70 * apple;
//Total sales
int totalSales = tokyo + kanagawa + tiba;
//Display of total sales
System.out.println(totalSales);
}
}
Execution result
It may be difficult to understand, but I think you will understand it as you study.
https://book.impress.co.jp/books/1113101090
Next time, I would like to write about "instruction execution statements"!
Recommended Posts