What I learned in Java (Part 2) What are variables?

Looking back on Java

I would like to take a quick look back at what I have been studying Java since the last time.

What is a variable?

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. スクリーンショット 2020-11-06 121659.png

As an example

public class Main {
	public static void main(String[] args) {
		int age;
		age = 20;
		System.out.println(age);
	}
}
  1. Create variable "age"
  2. Put the integer "20" in "age" (substitution)
  3. Display the contents of "age" on the console The image looks like this! image.png I would like to explain in order from 1.

Variable declaration

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. スクリーンショット 2020-11-06 225242.png 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". スクリーンショット 2020-11-06 125221.png

Data assignment

int age;
age = 20;

Substitute the integer "20" in the created variable "age". スクリーンショット 2020-11-06 142022.png 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;

Data reference

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. スクリーンショット 2020-11-06 145439.png 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 スクリーンショット 2020-11-06 143155.png As the name "number" changes, it will be overwritten with the most recently assigned one. スクリーンショット 2020-11-06 160755.png

Data type

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!

Primitive and reference types

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) スクリーンショット 2020-11-06 142022.png 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. スクリーンショット 2020-11-06 234019.png Instead of entering the "character string" directly, ... スクリーンショット 2020-11-06 235437.png Specifically, one cushion is already included, but as shown in the figure, the first address of the "character" sequence of the name is substituted.

Why use variables?

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 スクリーンショット 2020-11-08 232255.png 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 スクリーンショット 2020-11-08 232335.png

It may be difficult to understand, but I think you will understand it as you study.

References

https://book.impress.co.jp/books/1113101090

Next time, I would like to write about "instruction execution statements"!

Recommended Posts

What I learned in Java (Part 2) What are variables?
What I learned in Java (Part 3) Instruction execution statement
What I learned in Java (Part 4) Conditional branching and repetition
What I learned when building a server in Java
What I learned with Java Gold
What I learned with Java Silver
What i learned
What I learned from Java monetary calculation
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
When there are environment variables in Java tests
What I learned ② ~ Mock ~
Summary of what I learned in Spring Batch
A quick review of Java learned in class part4
A quick review of Java learned in class part3
[Rilas] What I learned in implementing the pagination function.
A quick review of Java learned in class part2
[Java Silver] What are class variables instance variables and local variables?
I tried to find out what changed in Java 9
What I researched about Java 8
I made roulette in Java.
[Environment variables] What are rails environment variables?
What are practically final variables?
What I researched about Java 7
I tried metaprogramming in Java
What are Java metrics? _Memo_20200818
What I learned about Kotlin
What I researched about Java 5
If variables are no longer highlighted in Eclipse's Java editor
What I learned from studying Rails
I sent an email in Java
Why are class variables needed? [Java]
I created a PDF in Java.
Java array variables are reference types
I wrote Goldbach's theorem in java
[Java] What got caught in encapsulation
I made an annotation in Java.
I tried using JWT in Java
How to name variables in Java
Creating lexical analysis in Java 8 (Part 2)
What are the rules in JUnit?
What I researched about Java learning
Java local variables are thread safe
Creating lexical analysis in Java 8 (Part 1)
[Java] What are overrides and overloads?
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 10)
[java] What I did when comparing Lists in my own class
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 7)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 3)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 9)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 6)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 4)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 5)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 2)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 1)
What I learned from doing Java work with Visual Studio Code
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 11)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 12)
Summary of "Design Patterns Learned in Java Language (Multithread Edition)" (Part 8)
[Java] I participated in ABC-188 of Atcorder.