Can you tell me what are the advantages (if there is any advantage) of using getTheLastElement2()
instead of getTheLastElement()
? I mean why is it necessary to create the reference obj
when it is easier just to return the result?
import java.util.ArrayList;
import java.util.List;
public class Test {
List list;
Test(ArrayList list){
this.list = list;
}
public Object getTheLastElement(){
if (list.isEmpty())
return null;
else
return list.get(list.size()-1);
}
public Object getTheLastElement2(){
Object obj;
if (list.isEmpty())
obj = null;
else
obj = list.get(list.size()-1);
return obj;
}
}