[英]Is there a performance benefit to local variables vs. class/instance variables in Java (Android)?
在 Android 中编程时,我经常在我的班级中将变量定义为类/实例变量(编辑前为“全局”,感谢您的澄清)。 如果我稍后需要访问它,请在onCreate()
分配它后在另一种方法中说。
对于我以后实际上没有访问它们的情况,Android Studio 的 Lint 代码检查会抛出警告,指出“字段可以转换为局部变量”。
我知道无论哪种方式我都会获得相同的功能,但是与将它们声明为类中的私有类/实例变量相比,Java 中的内联/本地方法变量(特别是在 Android 上,但也是在一般情况下)是否有任何性能或安全优势?
编辑/澄清: “全局”是指在课堂范围内。 (我所知道的被称为“类”或“实例”变量,我不好)可由类中的所有方法访问,而不是内联或特定于方法的变量。 也许某种示例代码可以说明我的观点。 前任:
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'
}
}
}
这有什么性能优势? 如果稍后我发现在添加功能或更改某些内容时需要在另一种方法中使用myPrivateVariable1
或myPublicVariable1
,那么编写使用数据的新方法(如果它们已经保存到定义的类变量并分配了值)会更容易来自onCreate()
方法。 如果变量是大数据集,唯一的好处是内存分配只会显着影响性能吗? 在这方面,公共和私人之间有什么区别?
在 Android 中编程时,我经常在我的班级中将变量定义为全局(私有)。 如果我稍后需要访问它,请在 onCreate() 中分配它之后在另一个方法中说。
你的意思是Class Scope
变量的一个术语。
我知道无论哪种方式我都会获得相同的功能,但是与在类中将它们声明为私有全局变量相比,Java 中的内联/本地方法变量(特别是在 Android 上,但也是一般的)是否有任何性能或安全优势?
使用方法范围变量的主要好处是可维护性。 考虑以下具有类作用域变量的类:
public class SampleClass {
// a class scope variable
private int mHeight;
private int getSquareValueOfHeight() {
return mHeight * mHeight;
}
private void increaseHeightByOne() {
mHeight = mHeight + 1;
}
}
我们有两种方法; getSquareValueOfHeight()
其读出的值mHeight
并返回它的平方值,并且increaseHeightByOne()
其修饰的值mHeight
。
您可以看到,无论何时需要更改这两种方法,都需要检查mHeight
。 如果有 3 或 5 种或更多方法访问mHeight
呢? 您必须重新检查所有方法,以确保更改不会破坏您的整个代码。
现在考虑以下类:
public class SampleClass {
private int height;
private int getSquareValueOfHeight(int value) {
int height = value * value;
return;
}
private int increaseHeightByOne(int height) {
return height + 1;
}
}
我们有两种使用其参数值的方法。 getSquareValueOfHeight()
将返回一个平方值而不修改类范围的height
变量,因为它有自己的height
变量(这是一种阴影机制)。 当您调用以下代码时:
SampleClass sampleClass = new SampleClass();
int value = sampleClass.getSquareValueOfHeight(5);
类范围变量height
不会改变。 因此,您不必担心更改会破坏您的整个代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.