In Java, can we claim that lambda expressions are stored and is present in heap? -


1. public interface mycomparator { 2.    public boolean compare(int a1, int a2); 3. } 4. myclass obj = new myclass(); 5. mycomparator mycomparator = (a1, a2) -> return a1 > a2; 6. boolean result = mycomparator.compare(2, 5); 

in line number 4, obj reference refers object on heap, constructed invoking myclass(). in line number 5, mycomparator reference referring present on other side of assignment operator, i.e lambda expression.

  • is lambda expression object? if yes, stored on heap? adhere rules of garbage collector, cleans unreferenced objects or behaves different?

  • if no, i.e if lambda expression not object , thereby assuming not present in heap, how mycomparator (being reference, assuming present in stack) able refer lambda expression , able invoke method on it?

  • in java, arrays stored on heap, can safely claim below array stored on heap? can safely assume 'code run' getting stored objects on heap?

    filefilter myfilefilter[] = new filefilter[] {  f -> f.exists(), f -> f.canread(), f -> f.getname().startswith("q")      } 
  • if lambda expressions can treated object, can serialize object , transport jvm, allowing send 'executable' code 1 jvm jvm, @ runtime? allow 'accept' logic system execute logic on system b. aside, might become possible distribute 'code' other systems (similar serializing runnable thread , sending across)? thinking in right path, please clarify whether these possibilities present. (would interested see implementation details )

from jls section 15.27:

evaluation of lambda expression produces instance of functional interface

so lambdas objects (at least now). because no exception made lambdas, lambdas treated other objects , stored on heap.

if remember correctly, way lambdas implemented (the actual lambda code generated @ runtime) allow representation change jvm changes, may change in future. example, stateless lambdas use of features used implement value types, , possibly end in special area of memory distinct "regular" heap.

this implementation allows interesting way of serializing lambda: serialize stuff needed generate lambda instead of generated code itself. lets lambdas evolve relatively independently of jvm first created in, , more flexible fixed stream of machine instructions.

as last question, don't know , can't know. "standard" answer yes, on heap, arrays , lambdas objects, wouldn't surprised if compiler optimize locally created arrays (and other objects) stack entities. don't know if transformation possible/feasible/implemented, though.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -