问题描述
与其他平台一样,跟进这个问题似乎合乎逻辑:Java中常见的非明显错误是什么?似乎他们应该工作的事情,但不要。
In the same spirit of other platforms, it seemed logical to follow up with this question: What are common non-obvious mistakes in Java? Things that seem like they ought to work, but don't.
我不会给出如何构建答案的指导,或者说太容易的指导被认为是一个问题,因为这就是投票的目的。
I won't give guidelines as to how to structure answers, or what's "too easy" to be considered a gotcha, since that's what the voting is for.
参见:
- Perl - Common gotchas
- .NET - Common gotchas
推荐答案
"a,b,c,d,,,".split(",").length
返回4,不 7,因为你可能(我当然 )期待。 split
忽略返回的所有尾随空字符串。这意味着:
returns 4, not 7 as you might (and I certainly did) expect. split
ignores all trailing empty Strings returned. That means:
",,,a,b,c,d".split(",").length
返回7!为了得到我认为的最不惊人的行为,你需要做一些非常惊人的事情:
returns 7! To get what I would think of as the "least astonishing" behaviour, you need to do something quite astonishing:
"a,b,c,d,,,".split(",",-1).length
得到7。
这篇关于Java - 常见问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!