Some language features in ABAP and Java about the default mechanism

Source: Internet
Author: User

New syntax for ABAP 740:

Code 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.

This shows that the new syntax is relatively concise, can write less than 3 lines of code. However, there is a flaw in the new syntax, if the Object_ext value of cl_crm_prodil_bo_names=>gc_prod_root is not present in the It_data table, the program execution is aborted and an exception is thrown cx_sy_itab_ Line_not_found.

In this case, ABAP also has a corresponding solution.

The 17th line throws an exception, and 19 rows are not, semantically easy to understand: If a record of name Spring2 does not exist in the inner table Lt_data, then the developer uses the default keyword to specify a struct as the defaults.

There are similar mechanisms in Java 8. To avoid the infamous Nullpointerexception,java developer, before using an instance method of an object, you need to check that the object instance is not NULL:

ifnull ){    xx.doSomething();}

In Java 8, a new tool class optional is provided in the package java.util to see its usage.

The 11th row of the filter method should semantically return a null because the length of the string constant that I initialized on line 10th is obviously greater than 6. But the object that invokes the filter method is a optional object, so the final filter method eventually returns a optional object that wraps a null. As shown in the debugger:

The result of the execution of line 14th prints out the value specified in OrElse, where OrElse's ideas are consistent with the default keywords in the ABAP described above.

The implementation of this optional tool class is also simple, and OrElse is just a ternary expression.

To see more language feature comparisons between ABAP and Java,javascript, please follow Jerry's public number: Wang Zixi.

Some language features in ABAP and Java about the default mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.