New syntax for ABAP 740:

The code above is equivalent to:

DATA: ls_data LIKE LINE OF it_data.
READ TABLE it_data INTO ls_data WITH KEY object_ext = cl_crm_prodil_bo_names=>gc_prod_root.
CALL METHOD add_data
   EXPORTING
      ir_child_object = ir_root_object
      is_data = ls_data.
Copy the code

This shows that the new syntax is simpler and saves three lines of code. The new syntax terminates the program if there is no record of object_ext with the value cl_CRM_PRODIL_BO_names =>gc_prod_root in the IT_data table. Raise an exception CX_SY_ITAB_LINE_NOT_FOUND.

ABAP also has a solution for this situation.

Line 17 in the figure below throws an exception, but line 19 does not, and is semantically easy to understand: if there is no record with name Spring2 in the inner table LT_data, a DEFAULT structure specified by the developer using the DEFAULT keyword is returned.

A similar mechanism exists in Java 8. To avoid the infamous NullPointerException, Java developers need to check that object instances are not NULL before using object instance methods:

if( xx ! =null ){
    xx.doSomething();
}
Copy the code

In Java 8, a new utility class named Optional is available in the java.util package to see how it can be used.

The filter method on line 11 should semantically return null, since the string constant I initialized on line 10 is obviously longer than 6. But the object that calls the filter method is an Optional object, so eventually the filter method also returns an Optional object wrapped around a NULL. As shown in the debugger below:

The result of line 14 prints the value specified in orElse, which is the same as the DEFAULT keyword in ABAP described above.

The Optional utility class is also easy to implement. OrElse is just a ternary expression.

For more information about ABAP, Java and JavaScript, please follow Jerry’s official account: Wang Zixi.