Java 使用变量而不是长语句?

Java 使用变量而不是长语句?,java,Java,我在处理对象时遇到了困惑。我在谷歌上搜索过,但找不到真正要搜索的单词。问题是: 我正在处理由其他对象组成的对象。例如: public void mapObjects(A a, B b) { a.setWeight(BigDecimal.valueOf(b.getWeight)); //Now my doubt lies here if (a.getCharges.getDiscounts.getDiscountList != null) { for(int

我在处理对象时遇到了困惑。我在谷歌上搜索过,但找不到真正要搜索的单词。问题是:

我正在处理由其他对象组成的对象。例如:

 public void mapObjects(A a, B b) {
    a.setWeight(BigDecimal.valueOf(b.getWeight));
    //Now my doubt lies here
    if (a.getCharges.getDiscounts.getDiscountList != null) {
      for(int i = 0; i < a.getCharges.getDiscounts.getDiscountList.size(); i++){
          b.getList().get(0).setDiscountValue(a.getCharges.getDiscounts.getDiscountList.get(i).getValue());
          b.getList().get(0).setDiscountName(a.getCharges.getDiscounts.getDiscountList.get(i).getValue);
      }
    }

}
公共void映射对象(A、B){
a、 设定重量(BigDecimal.valueOf(b.getWeight));
//现在我的怀疑就在这里
如果(a.getCharges.Get折扣.getDiscountList!=null){
对于(int i=0;i
上面的代码只是一个示例。我工作的项目使用类似类型的编码风格。使用
a.getCharges.getDiscounts.getDiscountList()
这类代码总是让我感到厌烦。因为我一次又一次地呼唤同一种说法

当我问一位大四学生为什么不把这个语句保存到一个简单的列表变量中。他告诉我,它将使用额外的引用,这将增加开销。使用变量比一次又一次地调用getter的开销大吗?

指出您的上级。如果它适用于Android上的有限资源,那么我猜将for循环中使用的所有内容存储在局部变量中的技术实际上对任何地方的性能都是有益的

在下面的摘录中,请注意,我们甚至没有提到调用(virtual)
list.size()
方法所带来的开销,只将
array.length
存储为局部变量会产生显著的性能差异

public void zero() {
    int sum = 0;
    for (int i = 0; i < mArray.length; ++i) {
        sum += mArray[i].mSplat;
    }
}

public void one() {
    int sum = 0;
    Foo[] localArray = mArray;
    int len = localArray.length;

    for (int i = 0; i < len; ++i) {
        sum += localArray[i].mSplat;
    }
}

public void two() {
    int sum = 0;
    for (Foo a : mArray) {
        sum += a.mSplat;
    }
}
public void zero(){
整数和=0;
对于(int i=0;i
zero()是最慢的,因为JIT还不能优化通过循环每次迭代获得一次数组长度的成本

一个更快。它将所有内容提取到局部变量中,避免查找。只有阵列长度提供了性能优势

对于没有JIT的设备,two()是最快的,对于具有JIT的设备,two()与one()是无法区分的。它使用Java编程语言1.5版中引入的增强for循环语法


只需将
折扣列表
字段设置为从不为空(即初始化为空列表),并对其进行迭代即可。比如:

for (Discount discount : a.getCharges().getDiscounts().getDiscountList()) {
    b.getList().get(0).setDiscountValue(discount.getValue());
    b.getList().get(0).setDiscountName(discount.getName());
}

你的“前辈”可能需要做一些研究。这样做的“性能影响”是每个对象几字节,每次访问几微秒。如果他真的对内存很感兴趣,用一个几乎没有内存占用的LinkedList初始化它。

在Java中,指向对象实例的变量
V
就是指向存储对象数据的内存位置的数值

当我们将
V
分配给另一个变量
V1
时,所发生的一切就是
V1
现在指向存储
O
数据的同一内存位置。这意味着,当您执行简单的赋值时,不分配“强”>新内存< /强>,其中C++代码> <代码>=/Cux>运算符可以重载,以进行深度复制,在这种情况下,实际上分配了新内存。下面用一个例子说明

考虑下面这样一个类

class Foo {

   private List<String> barList = new ArrayList<>();

   //class methods...

  //getter for the list
  List<String> getBarList() {
      return this.barList;
  }
}

public static void main(String[] args) {

   Foo f = new Foo()
   //the below lien will print 0 since there is no bar string added
   System.out.println("Bar list size: " + f.getBarList().size());

  // add a bar string. Observe here that I am simply getting the list 
  // and adding - similar to how your code is currently structured
  f.getBarList().add("SomeString");
  //now get a reference to the list and store it in a variable. 
  // Below anotherList only points to the same memory location 
  // where the original bar list is present. No new memory is allocated.
  List<String> anotherList = f.getBarList();
  //print the content of bar list and another list. Both will be same
  for(String s : f.getBarList()) {
     System.out.println(s);
  }
  for(String s: anotherList) {
    System.out.println(s);
  }

  //add a new bar string using the reference variable
  anotherList.add("Another string");
  //print the content of bar list and another list. Both will be same. If anotherList had separate memory allocated to it then the new string added would be only seen when we print the content of anotherList and not when we print the content of f.getBarList(). This proves that references are only some numeric addresses that point to locations of the object on heap.
  for(String s : f.getBarList()) {
     System.out.println(s);
  }
  for(String s: anotherList) {
    System.out.println(s);
  }

}
class-Foo{
private List barList=new ArrayList();
//类方法。。。
//列表的getter
列表getBarList(){
返回此.bar列表;
}
}
公共静态void main(字符串[]args){
Foo f=新的Foo()
//由于未添加条形字符串,下面的留置权将打印0
System.out.println(“条列表大小:+f.getBarList().size());
//添加一个条字符串。注意这里我只是获取列表
//和添加-类似于代码当前的结构
f、 getBarList().add(“SomeString”);
//现在获取列表的引用并将其存储在变量中。
//下面的另一个列表仅指向相同的内存位置
//存在原始条形图列表的位置。未分配新内存。
List anotherList=f.getBarList();
//打印条形图列表和其他列表的内容。两者将相同
对于(字符串s:f.getBarList()){
系统输出打印项次;
}
对于(字符串s:另一个列表){
系统输出打印项次;
}
//使用引用变量添加新的条形图字符串
另一个列表。添加(“另一个字符串”);
//打印条形列表和另一个列表的内容。两者都是相同的。如果另一个列表分配了单独的内存,则添加的新字符串仅在我们打印另一个列表的内容时可见,而在我们打印f.getBarList()的内容时不可见。这证明引用只是指向堆上对象位置的一些数字地址。
对于(字符串s:f.getBarList()){
系统输出打印项次;
}
对于(字符串s:另一个列表){
系统输出打印项次;
}
}

希望这会有所帮助。

因为Java交换的是引用而不是实际对象,如果您获取一个局部变量,它只会在堆栈框架中添加一个引用变量条目

  • 这种记忆将非常少,几乎可以忽略不计
  • 该内存将在方法完成后释放,因为它是该方法的本地内存
尽管如此,如果使用局部变量,您可以获得显著的性能提升。您多次在循环中提取相同的信息

  • a.getCharges.getDiscounts.getDiscountList.size()
    被多次调用。它应该是一个局部变量
  • b.getList().get(0)
    被多次调用。信息技术