[Java] Let's declare variables used in the loop in the loop [Variables in the block]

Introduction

Not limited to for loops, while loops, if-else statements, try-catch, etc. There are various "blocks", but variables that are used only in them should be declared in the block.

Bad example

List<String> strList = new ArrayList<String>();
String str = new String();  //← This! !! !!

for (int i = 0 ; i <= 10 ; i++) {
  str = i + "Th";
  strList.add(str);
}

Since String str is declared outside the for loop, it can also be used outside the for loop. So it goes against the Java theory of "the scope is as small as possible!".

I asked the person who wrote this

There were about two people who wrote this way, so I asked, "Why did you write this way?" Then

I wonder if it will save memory because I use the same String str: sparkles:

It's not a wrong idea in the sense that you don't create a lot of useless variables, though: sweat_smile: However, it is NG: ng: because the scope expands unnecessarily.

Good example

List<String> strList = new ArrayList<String>();

for (int i = 0 ; i <= 10 ; i++) {
  String str = new String();
  str = i + "Th";
  strList.add(str);
}

Use String str only inside a for loop. Then, if you declare it in a for loop, the scope will be smaller.

Java has garbage collection for "free memory as soon as you finish using it", so a new String str is created and released for each for loop. Therefore, you don't have to be very conscious of memory.

Summary

Variables used only in the for loop are declared in the for loop.

Roughly speaking, if you get lost, try putting a variable inside the loop. If there is no error, you can leave it as it is.

bonus

In my case, I wonder if the program written in "Good example" will be organized so far.

List<String> strList = new ArrayList<String>();

for (int i = 0 ; i <= 10 ; i++) {
  strList.add(i + "Th");
}

Recommended Posts

[Java] Let's declare variables used in the loop in the loop [Variables in the block]
Do not declare variables in List in Java
[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
About the meaning of type variables, E, T, etc. used in generics used in Java
Source used to get the redirect source URL in Java
Let's make a calculator application in Java ~ Display the application window
Access the network interface in Java
Guess the character code in Java
Specify the java location in eclipse.ini
Let's use Twilio in Java! (Introduction)
Unzip the zip file in Java
Parsing the COTOHA API in Java
Expression used in the fizz_buzz problem
How to name variables in Java
Let's create a TODO application in Java 5 Switch the display of TODO
Call the super method in Java
Specify the default JAVA_HOME used in buildship
Get the result of POST in Java
Java reference to understand in the figure
Now let's recap the Java lambda expression
Try using the Stream API in Java
Call the Windows Notification API in Java
I tried the new era in Java
[Java] Use cryptography in the standard library
Organized memo in the head (Java --Array)
[Processing × Java] How to use the loop
Try calling the CORBA service in Java 11+
What is the main method in Java?
How to get the date in java
The story of writing Java in Emacs
Console input in Java (understanding the mechanism)
Let's make a calculator application with Java ~ Create a display area in the window
Let's refer to C ++ in the module of AndroidStudio other project (Java / kotlin)
Regarding the transient modifier and serialization in Java
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
About the confusion seen in startup Java servers
The story of making ordinary Othello in Java
[LeJOS] Let's control the EV3 motor with Java
About the idea of anonymous classes in Java
ChatWork4j for using the ChatWork API in Java
A story about the JDK in the Java 11 era
Organized memo in the head (Java --Control syntax)
The intersection type introduced in Java 10 is amazing (?)
Use the JDK used in Android Studio in the terminal
The story of learning Java in the first programming
Measure the size of a folder in Java
When there are environment variables in Java tests
About var used in Java (Local Variable Type)
Feel the passage of time even in Java
Organized memo in the head (Java --instance edition)
Let's create a super-simple web framework in Java
Let's keep this in mind What's new in Java 9
What is the volatile modifier for Java variables?
What I learned in Java (Part 2) What are variables?
[Java] Read the file in src / main / resources
Organized memo in the head (Java --Data type)
Display "Hello World" in the browser using Java
[Java] Judgment by entering characters in the terminal
Display "Hello World" in the browser using Java
Try using the COTOHA API parsing in Java