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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Representing the Impractical and Impossible with JDK 10 “var”

105 点作者 pents90大约 7 年前

9 条评论

pents90大约 7 年前
While I submitted this, I would like to voice my opinion that I am against &quot;var&quot; in Java. People may ask, &quot;Why should I have to enter in the type if the compiler can infer it for me?&quot; My answer is twofold: 1) You or some other maintainer will need to know what that type is later when reading the code. Of course, &quot;var&quot; is meaningless, requiring you to dig back one or more steps to determine the actual type. 2) You don&#x27;t actually need to enter in the type, any competent IDE can do it for you.<p>So I&#x27;m not sure what we are saving here. When has the time spent typing in code ever been a bottleneck in software development anyways?<p>This is my feeling from having worked extensively in Java as well as languages that support &quot;var&quot;: C# and Swift. I feel like my productivity goes down when I have to support code that uses inferred types. There also seems to be a performance hit when compiling code with inferred typing, although that may be circumventable with better compiler tech, who knows.
评论 #16516202 未加载
评论 #16516122 未加载
评论 #16516204 未加载
评论 #16516081 未加载
评论 #16516073 未加载
评论 #16516447 未加载
评论 #16516094 未加载
评论 #16516155 未加载
评论 #16517290 未加载
评论 #16517658 未加载
评论 #16516739 未加载
评论 #16516474 未加载
评论 #16516746 未加载
评论 #16516313 未加载
评论 #16520248 未加载
评论 #16517491 未加载
评论 #16519485 未加载
评论 #16517478 未加载
评论 #16515988 未加载
评论 #16516416 未加载
评论 #16518106 未加载
评论 #16516471 未加载
评论 #16516069 未加载
评论 #16517555 未加载
vbezhenar大约 7 年前
I&#x27;m very disappointed that they didn&#x27;t make `val` keyword. Very simple change but code becomes significantly more readable and a lot of bugs will be compile errors.
评论 #16517187 未加载
评论 #16519701 未加载
评论 #16516622 未加载
saagarjha大约 7 年前
The “impossible” part of the title is an interesting one, since it mirrors how the arrival of lambdas coincided with the arrival of auto in C++ because such types are similarly impossible to represent. As I’ve mentioned in another comment in this thread, I think Java and C++ are very similar with regards for the need of type inference because they end up having a lot of the same issues.
ScottBurson大约 7 年前
This will be nice, but what I really want in Java is a type alias feature (what C++ calls &quot;typedef&quot;). This would be particularly helpful for function parameter and return types, which &#x27;var&#x27; won&#x27;t help with.
评论 #16517689 未加载
评论 #16517729 未加载
Insanity大约 7 年前
It&#x27;s one of the things I really missed when going from C# to Java, but with IntelliJ, the typing experience is very similar from C#.<p>Imagine I want to have a variable like:<p><pre><code> Person p = new Person(&quot;John&quot;); </code></pre> All I would type in IntelliJ is:<p><pre><code> new Person(&quot;John&quot;).var </code></pre> After pressing &quot;tab&quot;, that will autocomplete for me and put the focus on the variable name so I can rename it from the default inferred value.
评论 #16516866 未加载
评论 #16517241 未加载
rzzzt大约 7 年前
If you&#x27;d like to try how code looks with the use of this keyword, Lombok has support for &quot;var&quot;: <a href="https:&#x2F;&#x2F;projectlombok.org&#x2F;features&#x2F;experimental&#x2F;var" rel="nofollow">https:&#x2F;&#x2F;projectlombok.org&#x2F;features&#x2F;experimental&#x2F;var</a>
walshemj大约 7 年前
Can some explain the fist example says infers the type is right there in black and white<p>var foo = new ArrayList&lt;String&gt;();<p>It literally says exactly what foo is !!
评论 #16517847 未加载
exabrial大约 7 年前
My opinion is it you have the time to declare a variable, then you have the time to hit control-space to put the type. <i>Not</i> putting the type communicates less information to the next guy. If you &quot;don&#x27;t care&quot; about the type, then why are you declaring a variable in the first place? Just chain the call.
lkjlakjsdlfkj大约 7 年前
&quot;let&quot; is more elegant IMO.
评论 #16516732 未加载
评论 #16516499 未加载
评论 #16516620 未加载