Definition statement
The definition statement declares and initializes a variable at the same time.
Table of contents:
Format
The format is:
Class-name Variable-name = Expression ;
The left side of the assignment symbol is a variable declaration.
It initializes the variable with an instance returned by the expression on the right side of the assignment symbol.
Class name
It represents a class that is bound to a variable.
Variable name
This is an identifier used to access a variable.
Assignment symbol =
This is a symbol for setting a value in a variable.
Expression
The expression creates an instance that is set to the variable. The class of generated instance must be the same as the class name.
The class name on the left side and the class of the expression must match. However, there is a case that the class does not have to match in the definition statement of the numeric class.
If the class of the left side is a numeric class with a large range and the instance returned by the expression is a numeric class with a smaller range, it is allowed as a definition statement.
real rr = 10L; // Define real with long.
real rrr = 10; // Define real with int.
long ll = 10; // Define long with int.
In this case, the class on the left side is newly generated based on the instance returned by the expression.
Semicolon
The end of a definition statement is represented by a semicolon.
Example
1: | class example |
2: | int var1;// ♦1 |
3: | open method void someMethod() |
4: | int var2 = 10; // ♦2 |
5: | if(true) |
6: | int var3 = new int(100); // ♦3 |
7: | ei |
8: | em |
9: | ec |