java Java中双波浪号(~~)是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29736952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of double tilde (~~) in Java?
提问by Halle Knast
When browsing the source code of Guava, I came across the following piece of code (part of the implementation of hashCodefor the inner class CartesianSet):
在浏览Guava的源码时,遇到了如下一段代码(hashCode为内部类的实现部分CartesianSet):
int adjust = size() - 1;
for (int i = 0; i < axes.size(); i++) {
adjust *= 31;
adjust = ~~adjust;
// in GWT, we have to deal with integer overflow carefully
}
int hash = 1;
for (Set<E> axis : axes) {
hash = 31 * hash + (size() / axis.size() * axis.hashCode());
hash = ~~hash;
}
hash += adjust;
return ~~hash;
Both of adjustand hashare ints. From what I know about Java, ~means bitwise negation, so adjust = ~~adjustand hash = ~~hashshould leave the variables unchanged. Running the small test (with assertions enabled, of course),
两者的adjust和hash是int秒。根据我对 Java 的了解,~意味着按位否定,因此adjust = ~~adjust并且hash = ~~hash应该保持变量不变。运行小测试(当然,启用断言),
for (int i = Integer.MIN_VALUE; i < Integer.MAX_VALUE; i++) {
assert i == ~~i;
}
confirms this. Assuming that the Guava guys know what they are doing, there must be a reason for them to do this. The question is what?
证实了这一点。假设 Guava 家伙知道他们在做什么,那么他们这样做肯定是有原因的。问题是什么?
EDITAs pointed out in the comments, the test above doesn't include the case where iequals Integer.MAX_VALUE. Since i <= Integer.MAX_VALUEis always true, we will need to check that case outside the loop to prevent it from looping forever. However, the line
编辑正如评论中所指出的,上面的测试不包括iequals的情况Integer.MAX_VALUE。由于i <= Integer.MAX_VALUE始终为真,我们需要在循环外检查这种情况以防止它永远循环。然而,该行
assert Integer.MAX_VALUE == ~~Integer.MAX_VALUE;
yields the compiler warning "Comparing identical expressions", which pretty much nails it.
产生编译器警告“比较相同的表达式”,这几乎可以解决它。
回答by yshavit
In Java, it means nothing.
在 Java 中,它没有任何意义。
But that comment says that the line is specifically for GWT, which is a way to compile Java to JavaScript.
但该评论说该行专门用于 GWT,这是一种将 Java 编译为 JavaScript 的方法。
In JavaScript, integers are kind of like doubles-that-act-as-integers. They have a max value of 2^53, for instance. But bitwise operatorstreat numbers as if they're 32-bit, which is exactly what you want in this code. In other words, ~~hashsays "treat hashas a 32-bit number" in JavaScript. Specifically, it discards all but the bottom 32 bits (since the bitwise ~operators only looks at the bottom 32 bits), which is identical to how Java's overflow works.
在 JavaScript 中,整数有点像 doubles-that-act-integers。例如,它们的最大值为 2^53。但按位运算符将数字视为 32 位,这正是您在此代码中想要的。换句话说,在 JavaScript 中~~hash表示“hash视为 32 位数字”。具体来说,它会丢弃除底部 32 位之外的所有位(因为按位运算~符只查看底部 32 位),这与 Java 溢出的工作方式相同。
If you didn't have that, the hash code of the object would be different depending on whether it's evaluated in Java-land or in JavaScript land (via a GWT compilation).
如果没有,对象的哈希码会有所不同,具体取决于它是在 Java 领域还是在 JavaScript 领域(通过 GWT 编译)进行评估。

