For a final variable, whether it is a class variable, an instance variable, or a local variable, the final modifier is used when the variable is defined, and the initial value is specified when the final class variable is defined, and the initial value can be determined at compile time. So the final variable is essentially no longer a variable, but a direct amount.
public class finaltest{public
static void Main (string[] args) {
<strong>final int a=5+2;
Final double B=1.2/3;
Final String str= "crazy" + "Java";
Final string book= "crazy java Handout" +99.0;</strong>
//The value of the following BOOK2 variable cannot be determined at compile time because of the invocation of the method the
final string book2= " Crazy Java Handout "+string.valueof (99.0); 1
System.out.println (book== "Crazy Java Handout 99.0");
System.out.println (book2== "Crazy Java Handout 99.0");
}
The bold code in the above program defines four final variables, and the program assigns the initial values to the four variables, and the specified initial value is either an arithmetic expression or a string concatenation operation. Even though a string concatenation operation contains an implicit type (converts a numeric value to a string), the compiler can still determine the values of a, B, str, and book four variables at compile time, so they are all "macro variables"
Macro variable: An important use of the final modifier is to define macro variables. When the final variable is defined and the initial value is specified for the variable, and the initial value can be determined at compile time, the final variable is essentially a "macro variable", and the compiler replaces all of the program's values with that variable directly.
On the surface, the 1-line code defines a book2 that is not much different from the book, but explicitly converts the value 99.0 to a string when the BOOK2 variable is defined. However, because the value of the variable needs to invoke the method of the string class, the compiler cannot determine the BOOK2 value at compile time, and BOOK2 will not be treated as a "macro variable" class.
For example, the following example:
public class finaltest{public
static void Main (string[] args) {
String s1= "Crazy Java";
String s2= "Crazy" + "Java";
System.out.println (S1==S2);
String str1= "Madness";
String str2= "Java";
<span style= "color: #ff0000;" >string s3=str1+str2;</span>
System.out.println (S1==S3);
}
For S3, its value is obtained by connecting STR1 and STR2. Because STR1/STR2 is only two ordinary variables, the compiler does not perform "macro substitution," so the compiler cannot determine the value of S3 at compile time, and will not let S3 point to "Crazy Java" cached in the string pool. This shows that S1==S3 will output false
To make S1==S3 output true is also very simple, as long as the compiler can perform a "macro substitution" on the str1 and str2 two variables, the compiler can determine the value of the S3 at compile time, and the S3 point to the "Crazy Java" cache in the string pool. The program is changed to the following form:
public class finaltest{public
static void Main (string[] args) {
String s1= "Crazy Java";
String s2= "Crazy" + "Java";
System.out.println (S1==S2);
<strong>final String str1= "madness";
Final string str2= "Java";</strong>
string s3=str1+str2;
System.out.println (S1==S3);
}
such as the following example:
public class finaltest{
final static String str1;
Final static String str2= "Java";
static {
str1= "Java";
}
public static void Main (string[] args) {
System.out.println (str1+str1== "Javajava");
System.out.println (str2+str2== "Javajava");
}
The above program defines two final class variables, but only str2 specifies the initial value when the variable is defined, STR1 specifies the initial value in the static initialization block, so the system does not make a macro substitution for str1, but it performs a "macro replacement" on STR2.