How does chained methods execute in java?

前端 未结 3 1718
清酒与你
清酒与你 2020-12-20 06:29

Here is my code :

result = method1().method2().method3();

I would like to know the execution hierarchy of the above code/

3条回答
  •  佛祖请我去吃肉
    2020-12-20 07:19

    I'm explaining hierarchy of the above code with small example.

    result = method1().method2().method3();
    

    Example:

    getYear().toString().trim(); //like method1().method2().method3()
    

    First will be execute get year() which returns a Integer:

    2016.toString().trim();
    

    Secound will be execute toString() method of integer class which returns an string:

    "2016".trim();
    

    In Last trimming the stringwith trim() method of string class.

提交回复
热议问题