Show TOC Start of Content Area

Syntax documentation Object Creation Locate the document in its SAP Library structure

Use

Only create objects if they are really required to avoid unnecessary use of CPU time and memory.

Avoiding Object Creation

Basic Code 

The code sample creates the objects ArrayList, HashMap and String although, depending on the value of addToList, they might not be required.

private Object addIt(Provider p, boolean addToList){

  List list = new ArrayList();

  Map map = new HashMap();

  String value = p.getValue();

  String key = p.getKey();

  if(addToList){

     list.add(key);

     return list;

  }else{

     map.put(key,value);

     return map;

  }

}

 

Optimized Code

The code sample creates the objects ArrayList, HashMap and String when it is certain that they are actually required, thus avoiding the unnecessary creation of objects.

private Object addIt(Provider p, boolean addToList){

  Object result = null;

  String key = p.getKey();

  if(addToList){

     List list = new ArrayList();

     list.add(key);

     result = list;

  }else{

     Map map = new HashMap();

     String value = p.getValue();

     map.put(key,value);

     result = map;

  }

  return result;

}

 

End of Content Area