Lamda_SquareExample

Lamda Expression example for square of int :

Output:

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 Lamda_SquareExample.java
Lamda_SquareExample.java:13: error: not a statement
Interfun i = n->{(n*n);}
^
Lamda_SquareExample.java:13: error: ‘;’ expected
Interfun i = n->{(n*n);}
^
2 errors

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

Important:

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