Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

lambda expression

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello ranchers !  i have 2 doubts !

1.  if i have an interface such as :

interface CheckTrait {
boolean test(int b);
}

and another interface as :

interface Check {
boolean test(int a);
}

and a lambda expression as follows :

Check x=(x)->(x==9)?true:false;

the above statement in bold, shows an error such as : variable x is already defined in method main. the conditional statement is redundant .

I don't understand why the error is shown because  x belongs to the lambda body and is a local variable of the lambda body right.
Then why should it interfere with 'x' of type Check .


2.    if i change the Check reference to y and add another statement as follows :  




why is it so ?? can somebody please explain .
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) The lambda variable counts as a variable. So Java is upset you have x being uses as a variable (type for X) and again as a lambda variable.

2) The scope of the variable x is just the lambda. So by the time you declare t, the original lambda variable x is out of scope and available for reuse.
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you mam
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mam , if a lambda expression statement is just simply given as :

(x)->(x==9)?true:false;

a new instance of the respective interface is not created right ? in fact it throws an error that <none> cannot be dereferenced .

only when this lambda expression is assigned to the respective interface reference variable, a new instance is created right ?

i was not very sure about this.

 
Jeanne Boyarsky
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. You can't just have a "floating lambda". It needs to assigned to a variable or passed to a method to give the compiler enough context to figure out what you want.
 
shambhavi sivan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you mam !
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic