简体   繁体   English

优化:全局字符串与调用枚举类

[英]Optimization: Global String vs. Calling Enum Class

I have this enum class, 我有这个枚举类,

public enum Test {
    A("A"),
    B("B"),
    C("C"),
    D("D");

    private final String test;

    Test(final String test) {
        this.test = test;
    }

    public String getTestCode() {
        return stateCode;
    }
}

In a unit test class, I have declared the following global variable, 在单元测试类中,我声明了以下全局变量,

private static final String A = "A";

Question: 题:

In the unit test class, multiple tests will use on the value of A. 在单元测试类中,将对A的值使用多个测试。

For efficiency, purely from a optimization perspective (no matter how small the optimization might be), which will be more efficient 为了提高效率,仅从优化角度考虑(无论优化有多小),这将更加高效

  • to call the globally declared variable, 调用全局声明的变量,
  • to call the enum class the following way 通过以下方式调用枚举类

     Test.A.getTestCode(); 

or does it not make any difference at all? 还是根本没有任何区别?

private static final String A = "A"; is compile-time constant which value will be inlined, so code like 是编译时常量,将内联该值,因此类似

String x = YourClass.A;

will be compiler as 将被编译为

String x = "A";

In case of 的情况下

String x = Test.A.getTestCode();

you will need to call getTestCode() before receiving "A" . 您需要在接收到"A"之前调用getTestCode()


You can check it using javap -c command which will allow us to see compiled version of our class. 您可以使用javap -c命令对其进行检查,这将使我们能够查看类的编译版本。 So for code like 所以对于像这样的代码

public class Demo {

    private static final String A = "A";

    void test1(){
        String x = Demo.A;
    }

    void test2(){
        String x = "A";
    }

    void test3(){
        String x = Test.A.getTestCode();
    }

}

javap -c Demo shows javap -c Demo节目

  public com.stackoverflow.Demo();
    Code:
       0: aload_0
       1: invokespecial #12                 // Method java/lang/Object."<init>":()V
       4: return

  void test1();
    Code:
       0: ldc           #8                  // String A
       2: astore_1
       3: return

  void test2();
    Code:
       0: ldc           #8                  // String A
       2: astore_1
       3: return

  void test3();
    Code:
       0: getstatic     #22                 // Field Test.A:LTest;
       3: invokevirtual #27                 // Method Test.getTestCode:()Ljava/lang/String;
       6: astore_1
       7: return

which means that methods test1 and test2 are in fact same, while test3 first needs to access A enum, then invoke its getTestCode method. 这意味着方法test1test2实际上是相同的,而test3首先需要访问A枚举,然后调用其getTestCode方法。

I'm a fan of enums because they give you more potential for expansion (like a Test.B and Test.C and other helper functions), but for clarity you may wish to maintain a class variable: 我喜欢枚举,因为它们为您提供了更大的扩展潜力(例如Test.B和Test.C以及其他辅助函数),但是为了清楚起见,您可能希望维护类变量:

Test testA = Test.A;

And then call 然后打电话

testA.getTestCode();

IMO, there isn't much of a difference, though, but if you're only doing this for a single variable it's not really worth it to use an enum. IMO,虽然没有太大的区别,但是如果仅对单个变量执行此操作,那么使用枚举确实不值得。 If it's a value that's never changing (it seems like it is), you're probably better off with the final variable as it makes it clear that the value should never change. 如果它是一个永远不变的值(看起来像是不变的),那么使用final变量可能会更好,因为它可以清楚地表明该值永远不会变化。 If you add a JavaDoc line above its declaration you can make that even clearer to anyone who is reading your code. 如果将JavaDoc行添加到其声明上方,则可以使正在阅读您的代码的人更加清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM