When is an unassigned expression a valid statement?

前端 未结 4 746
一生所求
一生所求 2020-12-20 11:23

I\'ve read Oracle\'s expressions tutorial and couldn\'t understand this.

It is well known that the following line of code is valid Java syntax:

new O         


        
4条回答
  •  -上瘾入骨i
    2020-12-20 11:46

    Line containing only

    new Object(); 
    

    or to be more precise

    new SomeClass(); 
    

    is acceptable, because code of SomeClass() constructor may be all we want.

    But in case of lines containing only

    "foo";
    

    or

    2;//or (2+3);
    

    compiler knows that beside creating/reusing String literal or integer literal this code doesn't do anything else, which means it is probably some kind of programmer mistake so compiler can't accept it.

提交回复
热议问题