Is it better to create a new object and return it or create the new object in the return statement?

后端 未结 6 1938
盖世英雄少女心
盖世英雄少女心 2021-01-05 12:24

For example:

public Person newPerson() {
  Person p = new Person(\"Bob\", \"Smith\", 1112223333);
  return p;
}

as opposed to:

<         


        
6条回答
  •  一向
    一向 (楼主)
    2021-01-05 13:14

    One is not more efficient than the other, but returning created object directly is probably cleaner since you're using less temporary variables. If you must use temporary variables, make it final and name it appropriately.

提交回复
热议问题