TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Java 8 Method Reference Evaluation

45 点作者 cnahr超过 8 年前

5 条评论

pdpi超过 8 年前
It&#x27;s much easier to keep track of what&#x27;s happening if you don&#x27;t try to write it as one-liners.<p>This:<p><pre><code> final Runnable newRef = new Counter()::show; </code></pre> is the same as this:<p><pre><code> final Counter counter = new Counter(); final Runnable newRef = counter::show; </code></pre> Which is roughly equivalent to this:<p><pre><code> final Counter counter = new Counter(); final Runnable newRef = () -&gt; { counter.show(); }; </code></pre> Which is, of course, not the same thing as<p><pre><code> final Runnable newLambda = () -&gt; { new Counter().show(); }; </code></pre> The rest of the differences follow naturally from this distinction.
评论 #12775440 未加载
评论 #12777419 未加载
Leon超过 8 年前
Something to add that would help on your examples, would be a non-static counter to the static inner class. I think that would really help convey when when multiple objects are created but the reference to the first class instance is kept. With the static count variable new object instantiation is tracked, but misses a detail on object references that is interesting.<p>Specifically for this example:<p><pre><code> System.out.println(&quot;\nVariable in method reference&quot;); obj = new Counter(); &#x2F;&#x2F; NPE if after method reference declaration! final Runnable varRef = obj::show; System.out.println(&quot;Running...&quot;); varRef.run(); obj = new Counter(); varRef.run(); </code></pre> obj::show was evaluated, so varRef should be pointing to the original instance method of show, even when called the second time. Which could potentially create a memory leak if someone is tracking the method references and recreating objects underneath unknowingly.<p>However if I am completely wrong about this please tell me! I have not gone through and run this code for confirmation, so I would be very happy to know if I&#x27;m mistaken. I&#x27;m going by memory without reference checking or testing. In either case if I am wrong or right - please put a note about this situation! It would help immensely and make your post even more interesting.<p>Thanks!<p>Leon
twic超过 8 年前
What&#x27;s correct here is that the target of a method reference is evaluated when the reference is evaluated.<p>What&#x27;s incorrect is that references to static (or other) variables are somehow treated differently. The author has misinterpreted their own code - see jwolfe&#x27;s and my comments on the post.
评论 #12774350 未加载
mdadm超过 8 年前
Google cache since it seems to be 404&#x27;ing: <a href="http:&#x2F;&#x2F;webcache.googleusercontent.com&#x2F;search?q=cache:20xO-ms-ek8J:news.kynosarges.org&#x2F;2016&#x2F;10&#x2F;23&#x2F;java-method-reference-evaluation&#x2F;+&amp;cd=1&amp;hl=en&amp;ct=clnk&amp;gl=us" rel="nofollow">http:&#x2F;&#x2F;webcache.googleusercontent.com&#x2F;search?q=cache:20xO-ms...</a>
therealidiot超过 8 年前
404?
评论 #12773982 未加载