简体   繁体   English

局部变量与 Java (Android) 中的类/实例变量相比是否有性能优势?

[英]Is there a performance benefit to local variables vs. class/instance variables in Java (Android)?

I often define variables as class/instance variables ('global' before edit, thanks for the clarification) in my class while programming in Android.在 Android 中编程时,我经常在我的班级中将变量定义为类/实例变量(编辑前为“全局”,感谢您的澄清)。 In case I need to access it later, say in another method after it's been assigned in onCreate() .如果我稍后需要访问它,请在onCreate()分配它后在另一种方法中说。

For the occasions where I don't actually access them later, Android Studio's Lint code inspection throws warnings stating that the "Field can be converted to a local variable".对于我以后实际上没有访问它们的情况,Android Studio 的 Lint 代码检查会抛出警告,指出“字段可以转换为局部变量”。

I know I will get the same functionality either way, but is there any performance or security benefit to inline/local method variables in Java (specifically on Android, but also in general) vs. declaring them as private class/instance variables within a class?我知道无论哪种方式我都会获得相同的功能,但是与将它们声明为类中的私有类/实例变量相比,Java 中的内联/本地方法变量(特别是在 Android 上,但也是在一般情况下)是否有任何性能或安全优势?

EDIT/CLARIFICATION: By 'global', I meant in the scope of the class.编辑/澄清: “全局”是指在课堂范围内。 (What I know understand to be referred to as 'class' or 'instance' variables, my bad) Accessible by all methods within the class and not an inline or method specific variable. (我所知道的被称为“类”或“实例”变量,我不好)可由类中的所有方法访问,而不是内联或特定于方法的变量。 Maybe a sort of example code will illustrate my point.也许某种示例代码可以说明我的观点。 EX:前任:

public class MyActivity {

//Android Studio Lint will throw Java Class structure 'Field can be local' warnings 
//which is why I'm referring to these variables as "global" 
    private SomeDataType myPrivateVariable1; //'Field can be converted to a local variable'
    public SomeDataType myPublicVariable1; //'Field can be converted to a local variable'
    private SomeDataType myPrivateVariable2; 
    public SomeDataType myPublicVariable2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity_layout);

        //assign data to variables from either intent bundle or preferences or w/e
        myPrivateVariable1 = SOME_DATA;  
        myPublicVariable1 = SOME_DATA;
        //AS will suggest change to:
        // SomeDataType myPrivateVariable1 = SOME_DATA;
        // SomeDataType myPublicVariable1 = SOME_DATA;

        myPrivateVariable2 = SOME_DATA;
        myPublicVariable2 = SOME_DATA;

        //AS Lint will throw warning that variables can be converted b/c only used here
        someMethod(myPrivateVariable1);
        someOtherMethod(myPublicVariable1); 

        //run a method that uses the variables
        myMethodUsingVariable2(input);
    }

    private void myMethodUsingVariable2(DataType input) {
        //access the variables in multiple methods will not trigger the warning
        if (input == something) {
            //do something with 'myPrivateVariable2' and 'myPublicVariable2'
        }
    }

}

What is the performance benefit to this?这有什么性能优势? If later I find I need to use either myPrivateVariable1 or myPublicVariable1 in another method as I add a feature or change something, it would be easier to write new methods that used the data if they were already saved to a defined class variable and assigned a value from the onCreate() method.如果稍后我发现在添加功能或更改某些内容时需要在另一种方法中使用myPrivateVariable1myPublicVariable1 ,那么编写使用数据的新方法(如果它们已经保存到定义的类变量并分配了值)会更容易来自onCreate()方法。 Is the only benefit memory allocation that will only significantly affect performance if the variables are large data sets?如果变量是大数据集,唯一的好处是内存分配只会显着影响性能吗? What would be the difference between public and private in that regards as well?在这方面,公共和私人之间有什么区别?

I often define variables as global (private) in my class while programming in Android.在 Android 中编程时,我经常在我的班级中将变量定义为全局(私有)。 In case I need to access it later, say in another method after it's been assigned in onCreate().如果我稍后需要访问它,请在 onCreate() 中分配它之后在另一个方法中说。

What you meant is a term for Class Scope variable.你的意思是Class Scope变量的一个术语。

I know I will get the same functionality either way, but is there any performance or security benefit to inline/local method variables in Java (specifically on Android, but also in general) vs. declaring them as private global variables within a class?我知道无论哪种方式我都会获得相同的功能,但是与在类中将它们声明为私有全局变量相比,Java 中的内联/本地方法变量(特别是在 Android 上,但也是一般的)是否有任何性能或安全优势?

The major benefit using a method scope variable is maintainability .使用方法范围变量的主要好处是可维护性 Consider the following class with class scope variable:考虑以下具有类作用域变量的类:

public class SampleClass {
  // a class scope variable
  private int mHeight;

  private int getSquareValueOfHeight() {
    return mHeight * mHeight;
  }

  private void increaseHeightByOne() {
    mHeight = mHeight + 1;
  }
}

we have two method;我们有两种方法; getSquareValueOfHeight() which read the value of mHeight and return the square value of it, and increaseHeightByOne() which modified the value of mHeight . getSquareValueOfHeight()其读出的值mHeight并返回它的平方值,并且increaseHeightByOne()其修饰的值mHeight

You can see that you need to check mHeight whenever you need to change both the methods.您可以看到,无论何时需要更改这两种方法,都需要检查mHeight How about if there are 3 or 5 or more methods accessing the mHeight ?如果有 3 或 5 种或更多方法访问mHeight呢? You have to recheck of all the methods just to make sure a change doesn't break your whole code.您必须重新检查所有方法,以确保更改不会破坏您的整个代码。

Now consider the following class:现在考虑以下类:

public class SampleClass {

  private int height;

  private int getSquareValueOfHeight(int value) {
    int height = value * value;
    return;
  }

  private int increaseHeightByOne(int height) {
    return height + 1;
  }

}

we have two methods that using a value from its parameter.我们有两种使用其参数值的方法。 The getSquareValueOfHeight() will return a squared value without modifying the class scope height variable because it has its own height variable (this is a shadowing mechanism). getSquareValueOfHeight()将返回一个平方值而不修改类范围的height变量,因为它有自己的height变量(这是一种阴影机制)。 When you're calling the following code:当您调用以下代码时:

 SampleClass sampleClass = new SampleClass();
 int value = sampleClass.getSquareValueOfHeight(5);

the class scope variable height won't be changed.类范围变量height不会改变。 So, you don't need to worry that a change will broke your whole code.因此,您不必担心更改会破坏您的整个代码。

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

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