Compile-time and run-time constants in Java

Source: Internet
Author: User

Constants are constant amounts of time during a program's run, and many programming languages have some way of telling the compiler that a piece of data is constant, such as const in C + + and final in Java.

According to the different behavior of the compiler, the constants are divided into compile constant and run -time, in fact, the compiler is always the usual amount of work, just compile the constant amount is executed when compiling the calculation, and bring into the program all may use it in the calculation formula.

In Java, for example, static final int a = 1 will be a compile-time constant, the compiled symbol table will not find a, all references to A are replaced by 1.
and static final int b = "". Length () will be a run-time constant. The test code is as follows:

class Test {public  static  final int  a =  Ten ; public  static  final int  b =  "Test" . Length (); static  {System. out . println ( "class Test was Loaded!" ); }}public  class  compilconstant {public  static  void  main  (string[] args) {system. Out . println (TEST.A); System. out . println (test.b); }} 

Output:

10Class Test Was Loaded !4

That
1. A is used as a compile-time global constant, does not depend on a class, and B is the global constant of the run time, and its value is dependent on the class.
2. Compile-time constants can be used to determine the value in the compilation, and a in the above example can determine the value, but B is not possible to determine the value at compile time.
3. Because compile-time constants do not depend on classes, access to compile-time constants does not cause the initialization of classes.

Test:

//System.out.println(Test.b);//注释掉该条代码

Perform

javac CompilConstant.javadel Test.class

That is, the compilation generated by the Test.class deletion, and execution java CompilConstant , output:
10

To perform disassembly:
Javap-v compilconstant

D:\N3verl4nd\Desktop>Javap- vCompilconstantclassfile/d:/n3verl4nd/desktop/compilconstant.Class Last Modified .-Ten- +; Size the bytesMD5 checksum faf8cf7e41ff58ae190d78fbb7e7635c Compiled from"Compilconstant.java" PublicClass Compilconstant minor version:0Major version: theFlags:acc_public, Acc_superconstant Pool:#1 =MethodRef#7.#16         //Java/lang/object. " <init> ":() V   #2 =Fieldref#17.#18        //Java/lang/system.out:ljava/io/printstream;   #3 =Class#19            //Test   #4 =MethodRef#20.#21        //JAVA/IO/PRINTSTREAM.PRINTLN: (I) V   #5 =Fieldref#3.#22         //Test.c:i   #6 =Class#23            //Compilconstant   #7 =Class#24            //Java/lang/object   #8 =Utf8<Init>   #9 =Utf8 () V#10 =Utf8 Code#11 =Utf8 linenumbertable#12 =Utf8 Main#13 =Utf8 ([ljava/lang/String;) V#14 =Utf8 sourcefile#15 =Utf8 compilconstant.Java#16 =Nameandtype#8:#9          //"<init>":() V  #17 =Class#25            //Java/lang/system  #18 =Nameandtype#26:#27        //Out:ljava/io/printstream;  #19 =Utf8 Test#20 =Class#28            //Java/io/printstream  #21 =Nameandtype#29:#30        //println: (I) V  #22 =Nameandtype#31:#32        //B:i  #23 =Utf8 compilconstant#24 =Utf8 Java/lang/object#25 =Utf8 Java/lang/system#26 =Utf8 out#27 =Utf8 Ljava/io/printstream;#28 =Utf8 Java/io/printstream#29 =Utf8 println#30 =Utf8 (I) V#31 =Utf8 b#32 =Utf8 i{ PublicCompilconstant (); Descriptor: () V flags:acc_public Code:Stack=1, locals=1, args_size=1         0: Aload_01: invokespecial#1                  //Method java/lang/object. " <init> ":() V         4:returnLinenumbertable:line9:0   PublicStaticvoidMain (Java.Lang.String[]); Descriptor: ([ljava/lang/String;) V flags:acc_public, acc_static Code:Stack=2, locals=1, args_size=1         0: getstatic#2                  //Field Java/lang/system.out:ljava/io/printstream;         3: BipushTen         5: invokevirtual#4                  //Method java/io/printstream.println: (I) V         8: getstatic#2                  //Field Java/lang/system.out:ljava/io/printstream;         One: getstatic#5                  //Field test.b:i         -: invokevirtual#4                  //Method java/io/printstream.println: (I) V         -:returnLinenumbertable:line One:0Line A:8Line -: -}sourcefile:"Compilconstant.java"D:\N3verl4nd\Desktop>

It can be seen that the output of test.a directly pushes bipush 10 10 into the stack.

reprinted from: n3verl4nd Source: 78303623 

Compile-time and run-time constants in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.