Lamda Expression example for square of int :


Square of 2 is :4

now suppose we put parenthesis in the body as below and try to compile the code using command prompt,

you will get compilation error as below:

\basic>javac error: not a statement
Interfun i = n->{(n*n);}
^ error: ‘;’ expected
Interfun i = n->{(n*n);}
2 errors

so if we are putting parenthesis then return statement and ; needs to be added.


Below are the various scenarios:

Interf i = n->n*n ;//valid

Interf i = (n)->n*n ;//valid

Interf i = n->return n*n ;//invalid as return is valid only inside curly braces

Interf i = n->{retrun n*n ;} ;//valid

Interf i = n->{retrun n*n ;} //invalid,syntax error

Interf i = n->{n*n;} ;//invalid as inside curly braces return is mandatory to provide.




Comments are closed