Studying Java-Part 2-Variables

1.First of all

This time, I will talk about variables. Variables are the basics of the basics in a program. That's important and needs to be remembered.

By the way, did you often use variables in mathematics? When x = 1, y = 5, answer z with z = 7x + 5y. Lol like This can be expressed in a similar expression in Java.

And the variables will be used in Java as they are. However, Java requires a process called "** declare **" the variables to be used. Simply put, it's like, "I'm going to use a variable called an integer number!" (The integer part will be understood later, so don't worry too much)

2. Declaration of variables

So let's actually declare the variable in the source. From there, the following part becomes the source code, and for the time being, I would like to describe the process between {} of public static void main (String [] args) {}. Other parts will be understood later. For now, it would be helpful if you ignore it.

Main.java


public class Main {
	public static void main(String[] args) {

		//Variable declaration
		int number;

	}
}

"Int number;" is the declaration. From now on, we will use a variable called number, which is an integer, so prepare a computer! It's like that lol (The line after // is called a comment and has nothing to do with the program.)

"** type variable name; **" Example) Type = int, variable name = number

This is the basic form of declaration. The type is int and the variable name is the number part. (By the way, int is a type for handling integers. Characters and decimals cannot be used. I will introduce it later.)

3. Substitution

And you can say "** assign a value **" to a variable. Something like "x = 1" or "y = 5" mentioned at the beginning.

Let's actually do it.

Main.java


public class Main{
  public static void main(String[] args){
 
    //Variable declaration
    int number;
    //Substitute 1 for number
    number = 1;
 
  }
}

I tried substituting "1" for number. (Nothing is displayed because there is no output) Numbers are called variables, whereas values written directly on the source "1" are called differently. The raw values, such as 1, 80, and 70, are called "** literals **".

You can also write declarations and assignments on one line. In this case, it is called "** Variable initialization **".

Main.java


public class Main {
	public static void main(String[] args) {

		//Declare a variable and assign 1 to number
		int number = 1;

	}
}

4. Variable output

Just assigning variables is not good, so let's output to standard output (console). The screen display is the previous [Study Java-Part 1-Hello World 4. Beginning of processing](https://qiita.com/koukonko/items/98d36c3bae2f5e119bd7#4-%E5%87%A6%E7%90 It also appeared in% 86% E3% 81% AE% E5% A7% 8B% E3% 81% BE% E3% 82% 8A).

"System.out.println ();" This is the one. If you put the variable that you declared and assigned earlier in this (), the value held by the variable will be displayed.

Main.java


public class Main {
	public static void main(String[] args) {

		//Declare a variable and assign 1 to number
		int number = 1;

		//Display the value of number on the screen
		System.out.println(number);

	}
}

When the above program is executed, "1" is displayed.

5. Change value

It's just a "variable", so let's change the value.

Main.java


public class Main {
	public static void main(String[] args) {

		//Declare a variable and assign 1 to number
		int number = 1;

		//Substitute 10 for number
		number = 10;

		//Display the value of number on the screen
		System.out.println(number);

	}
}

I added another line to the above program. It's the part of "number = 10;". In this one line, the value "1" contained in number will be changed to the value "10". Please note that "1" will be overwritten and erased ...

When I run it, the console shows "10". This is proof that number holds "10".

This is the basic usage of variables.

6. About the declaration

There are rules for declaring variables and the like. Variable names include names that cannot be used and names that are not recommended. In addition, we will give names to various things in the future, so I will introduce them as well. I'm sure there are some words you don't understand, but for the first time, it's through and lol

6.1. Declaration rules

The following rules must be followed, and an error will occur if you write them incorrectly.

6.1.1. Naming convention

Here are some rules that you must follow when naming. When naming, you can use "$", "_", "alphabet", and "number". However, "number" cannot be the first character. Also, you cannot use the reserved words below as names, or you cannot use names that you have already used.

OK example) \ _number, firstName, kou $ kon_ko NG example) 1_number, 100, "string"

6.1.2. Reserved words

A list of names that cannot be used due to Java syntax or other reasons. Also, null, true, false cannot be used.

Reserved word list
abstract assert boolean break byte
case catch char new class
const case continue default do
double else enum extends final
finally float for goto if
implements import instanceof int interface
long native new package private
protected public return short static
strictfp super switch synchronized this
throw throws transient try void
volatile while

6.2. Declaration Recommendations

The following is, but not always, for the sake of programming clarity. (Recommendations may be different depending on the development site.)

6.2.1. Variable name

If you use lowercase letters and use multiple words, capitalize the first letter of the second and subsequent words.

Example) story, member, bookStore, programNo

6.2.2. Class name

Capitalize the first letter of a word. Even if you use multiple words, the first letter is capitalized ...

Example) Computer, Navigator, Concreate Aggregate

6.2.3. Method name

If you use lowercase letters and use multiple words, capitalize the first letter of the second and subsequent words. This is also a naming method similar to variables.

It is commonly named as "verb" or "verb + noun". In "verb + noun", the first letter of "noun" is capitalized.

Example) wait, send ← "verb only" Example) getNumber, setName ← "verb + noun"

6.2.4. Constant name

Use all uppercase words. When using multiple words, connect them with "_".

Example) MAX_SIZE, FILE_NO, ARRAY_SIZE

7. Conclusion

That's all for now. It has become a little longer. I'm sorry for all the boring stories ...

From now on, it is necessary to remember as you proceed with your studies, but let's remember slowly.

Next time, I would like to explain about "types".

Next time → "Study Java-Part 3-Type"

Recommended Posts

Studying Java-Part 2-Variables
Studying Java-Part 0-Overview
Studying Java-Part 4-Literal
Studying Java-Part 5-Constant
Studying Java-Part 9-Scope
Studying Java-Part 7-Array
Studying Java-Part 3-Type
Studying Java-Part 10-if statement
Studying Java-Part 1-Hello World
Studying Java-Part 11-switch statement
Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10