Studying Java-Part 5-Constant

1.First of all

This time, I will explain about variables and constants that are opposite to each other.

Variables change in value. The constant is a fixed value.

It's like that. It is a constant that prevents you from changing the value later.

2. Declaration of constants

Now let's see how to declare a constant. We will also show you how to initialize it at the same time as the declaration.

Main.java


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

		//Declaration of constants&Initialization
		final int MAX_VALUE = 100;

	}
}

By prefixing the type with something called final, you are declaring it as a constant. In addition, the value is assigned and initialized at the same time as the declaration.

3. Use constants

As for how to use constants, they can be used as values like variables, just because they cannot be assigned. For example, as below

Main.java


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

		//"Constant MAX_Declaration of "VALUE"&Initialization
		final int MAX_VALUE = 100;

		//Declaration of "variable first"&Initialization
		int first = 200;

		//Declaration of "variable second"
		int second;

		//"Constant MAX_The sum of "VALUE" and "Variable first"(100 + 200)To "variable second"
		second = MAX_VALUE + first;

		//Output as 300
		System.out.println(second);

	}
}

If "300" is output, it's okay!

Roughly the flow of processing The value obtained by adding the constant "MAX_VALUE" and the variable "first" is assigned to the variable "second". Output the variable "second". Is it like that?

4. Check the constant

By the way, let's check if it is really a constant. When I compile the following program,

Main.java


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

		//Declaration of constants&Initialization
		final int MAX_VALUE = 100;

		//substitute
		MAX_VALUE = 123;

	}
}

You will get an error. Attempting to assign a value to a constant will result in an error at compile time.

It turns out that you can't assign a value later! Noshi

5. Conclusion

Constants are very useful to declare if you use the same literal in multiple places when writing a program.

For example, suppose you have a program that literally describes pi 3.14 100 times.

Main.java


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

		// 3.14 + n (I want to write n up to 100)
		System.out.println(3.14 + 1);
		System.out.println(3.14 + 2);

		//Omission

		System.out.println(3.14 + 99);
		System.out.println(3.14 + 100);

	}
}

~~ It's an almost impossible program, but it's just an example ~~

If you write it 100 times, you may make a mistake. (If you write 3.15, it will not cause a compile error, so it is very difficult to notice.) Furthermore, if you want to increase the number of digits to 3.14159265, you will have to correct 100 places.

Therefore, if you declare it as a constant, you can reduce mistakes by reusing the constant, and if you want to change it, you only have to change the value of the constant, so it will be strong against change.

Main.java


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

		//Declaration of the constant "PI"
		final double PI = 3.14;

		// 3.14 + n (I want to write n up to 100)
		System.out.println(PI + 1);
		System.out.println(PI + 2);

		//Omission

		System.out.println(PI + 99);
		System.out.println(PI + 100);

	}
}

Constants are also related to the readability of the source code. If you know the value is fixed from the beginning, use a constant.

Next time, I will explain an article about "operators". that's all.

Next time → "Study Java-Part 6-Operator"

Recommended Posts

Studying Java-Part 5-Constant
Studying Java-Part 0-Overview
Studying Java-Part 4-Literal
Studying Java-Part 2-Variables
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