Java“new String [-1]”通过编译。怎么来的? [英] Java "new String[-1]" passes compilation. How come?

查看:181
本文介绍了Java“new String [-1]”通过编译。怎么来的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在Java中进行挑战时,我初始化了一个新的长度为负的String数组。
ie -

  String [] arr = new String [-1]; 

令我惊讶的是,编译器没有抱怨。
Googling没有提出任何相关的答案。



非常感谢!

解决方案

p>

它是在 JLS 15.10.1 。以下是相关的片段:


...如果任何DimExpr表达式的值小于零,则NegativeArraySizeException抛出。


现在,如果Java编译器将代码标记为错误,那么指定的行为不会发生。 ..在那个特定的代码。



此外,没有任何文本可以找到授权编译器在显而易见的错误案例中涉及编译时常数表达式,如 -1







下一个问题,当然是为什么JLS允许这个?


你需要问Java设计师。但我可以想到一些(大部分)合理的原因:




  • 这是最初被忽视的,没有固定的。 (注意修复它破坏源代码兼容性。)


  • 它被认为是太不寻常/边缘情况值得处理。

    / li>
  • 这可能会对编写源代码生成器的人造成问题。 (想象一下,不得不编写代码来评估编译时常数表达式,以便不生成不可编译的代码。使用当前的JLS规范,您可以简单地生成具有坏大小的代码,并处理)


  • 也许有人计划在Java中添加unarrays: - )







其他答案建议编译器可以/应该标记这种情况。如果标记意味着输出警告消息,这肯定是由JLS允许的。然而,有人怀疑编译器是否应该这样做。一方面,如果上面的代码是错误写的,那么这个错误被标记是有用的。另一方面,如果它不是一个错误(或错误是不相关的),那么警告将是噪音,或更糟。无论哪种方式,这是你需要与相应编译器的维护者讨论的事情。


While fiddling around in Java, I initialized a new String array with a negative length. i.e. -

String[] arr = new String[-1];

To my surprise, the compiler didn't complain about it. Googling didn't bring up any relevant answers. Can anyone shed some light on this matter?

Many thanks!

解决方案

The reason is that the JLS allows this, and a compiler that flagged it as a compilation error would be rejecting valid Java code.

It is specified in JLS 15.10.1. Here's the relevant snippet:

"... If the value of any DimExpr expression is less than zero, then a NegativeArraySizeException is thrown."

Now if the Java compiler flagged the code as an error, then that specified behaviour could not occur ... in that specific code.

Furthermore, there's no text that I can find that "authorizes" the compiler to reject this in the "obvious mistake" cases involving compile-time constant expressions like -1. (And who is to say it really was a mistake?)


The next question, of course, is 'why does the JLS allow this?'

You've need to ask the Java designers. However I can think of some (mostly) plausible reasons:

  • This was originally overlooked, and there's no strong case for fixing it. (Noting that fixing it breaks source code compatibility.)

  • It was considered to be too unusual / edge case to be worth dealing with.

  • It would potentially cause problems for people writing source code generators. (Imagine, having to write code to evaluate compile-time constant expressions in order that you don't generate non-compilable code. With the current JLS spec, you can simply generate the code with the "bad" size, and deal with the exception (or not) if the code ever gets executed.)

  • Maybe someone had a plan to add "unarrays" to Java :-)


Other answers have suggested that the compiler could / should "flag" this case. If "flagging" means outputting a warning message, that is certainly permitted by the JLS. However, it is debatable whether the compiler should do this. On the one hand, if the above code was written by mistake, then it would be useful to have that mistake flagged. On the other hand, if it was not a mistake (or the "mistake" was not relevant) then the warning would be noise, or worse. Either way, this is something that you would need to discuss with the maintainer(s) for the respective compiler(s).

这篇关于Java“new String [-1]”通过编译。怎么来的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆