将double转换为Int,向下舍入(Convert double to Int, rounded down)
如何将
double
值转换为int
执行以下操作:Double If x = 4.97542. Convert to int x = 4. Double If x = 4.23544. Convert to int x = 4.
也就是说,答案总是四舍五入。
How to convert a
double
value toint
doing the following:Double If x = 4.97542. Convert to int x = 4. Double If x = 4.23544. Convert to int x = 4.
That is, the answer is always rounding down.
原文:https://stackoverflow.com/questions/10280520
最满意答案
是的,调用
getClass()
已经成为一个规范的“测试null
”成语,因为getClass()
被认为是一个便宜的内在操作,我想HotSpot可能能够检测到这种模式并将操作减少到一个内在的null
如果没有使用getClass()
的结果,则检查操作。另一个例子是用外部实例创建一个不是
this
的内部类实例:public class ImplicitNullChecks { class Inner {} void createInner(ImplicitNullChecks obj) { obj.new Inner(); } void lambda(Object o) { Supplier<String> s=o::toString; } }
编译
Compiled from "ImplicitNullChecks.java" public class bytecodetests.ImplicitNullChecks { public bytecodetests.ImplicitNullChecks(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return void createInner(bytecodetests.ImplicitNullChecks); Code: 0: new #23 // class bytecodetests/ImplicitNullChecks$Inner 3: dup 4: aload_1 5: dup 6: invokevirtual #24 // Method java/lang/Object.getClass:()Ljava/lang/Class; 9: pop 10: invokespecial #25 // Method bytecodetests/ImplicitNullChecks$Inner."<init>":(Lbytecodetests/ImplicitNullChecks;)V 13: pop 14: return void lambda(java.lang.Object); Code: 0: aload_1 1: dup 2: invokevirtual #24 // Method java/lang/Object.getClass:()Ljava/lang/Class; 5: pop 6: invokedynamic #26, 0 // InvokeDynamic #0:get:(Ljava/lang/Object;)Ljava/util/function/Supplier; 11: astore_2 12: return }
另请参见JDK-8073550 :
我们的类库中的一些地方使用使用object.getClass()来检查无效性的奇怪技巧。 虽然这似乎是一个明智的举动,但它实际上让人们相信这是一个经过验证的空检查实践。
在JDK 7中,我们有Objects.requireNonNull,它提供了适当的空检查,并正确地声明了意图。
这是否也适用于编程语言内部检查也许是有争议的,因为使用
Objects.requireNonNull
会为此目的创建对源代码中不可见的java.lang
包之外的类的依赖关系。 在这个特定的情况下,这个技巧只对那些查看字节码的人可见。 但是已经决定改变Java 9的行为。这就是
jdk1.9.0b160
如何编译相同的测试类:Compiled from "ImplicitNullChecks.java" public class bytecodetests.ImplicitNullChecks { public bytecodetests.ImplicitNullChecks(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return void createInner(bytecodetests.ImplicitNullChecks); Code: 0: new #26 // class bytecodetests/ImplicitNullChecks$Inner 3: dup 4: aload_1 5: dup 6: invokestatic #27 // Method java/util/Objects.requireNonNull:(Ljava/lang/Object;)Ljava/lang/Object; 9: pop 10: invokespecial #28 // Method bytecodetests/ImplicitNullChecks$Inner."<init>":(Lbytecodetests/ImplicitNullChecks;)V 13: pop 14: return void lambda(java.lang.Object); Code: 0: aload_1 1: dup 2: invokestatic #27 // Method java/util/Objects.requireNonNull:(Ljava/lang/Object;)Ljava/lang/Object; 5: pop 6: invokedynamic #29, 0 // InvokeDynamic #0:get:(Ljava/lang/Object;)Ljava/util/function/Supplier; 11: astore_2 12: return }
Yes, calling
getClass()
has become a canonical “test fornull
” idiom, asgetClass()
is expected to be a cheap intrinsic operation and, I suppose, HotSpot might be capable of detecting this pattern and reduce the operation to an intrinsicnull
-check operation, if the result ofgetClass()
is not used.Another example is creating an inner class instance with an outer instance that is not
this
:public class ImplicitNullChecks { class Inner {} void createInner(ImplicitNullChecks obj) { obj.new Inner(); } void lambda(Object o) { Supplier<String> s=o::toString; } }
compiles to
Compiled from "ImplicitNullChecks.java" public class bytecodetests.ImplicitNullChecks { public bytecodetests.ImplicitNullChecks(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return void createInner(bytecodetests.ImplicitNullChecks); Code: 0: new #23 // class bytecodetests/ImplicitNullChecks$Inner 3: dup 4: aload_1 5: dup 6: invokevirtual #24 // Method java/lang/Object.getClass:()Ljava/lang/Class; 9: pop 10: invokespecial #25 // Method bytecodetests/ImplicitNullChecks$Inner."<init>":(Lbytecodetests/ImplicitNullChecks;)V 13: pop 14: return void lambda(java.lang.Object); Code: 0: aload_1 1: dup 2: invokevirtual #24 // Method java/lang/Object.getClass:()Ljava/lang/Class; 5: pop 6: invokedynamic #26, 0 // InvokeDynamic #0:get:(Ljava/lang/Object;)Ljava/util/function/Supplier; 11: astore_2 12: return }
See also JDK-8073550:
A few places in our class library use the weird trick of using object.getClass() to check for nullity. While this make seem a smart move, it actually confuses people into believing this is an approved practice of null checking.
With JDK 7, we have Objects.requireNonNull that provide the proper null checking, and declare the intent properly.
It might be debatable whether this should apply to programming language intrinsic checks as well, as using
Objects.requireNonNull
for that purpose would create a dependency to a class outside thejava.lang
package not visible in the source code. And in this specific case, the trick is only visible to those who look at the byte code. But it has been decided to change the behavior with Java 9.This is how
jdk1.9.0b160
compiles the same test class:Compiled from "ImplicitNullChecks.java" public class bytecodetests.ImplicitNullChecks { public bytecodetests.ImplicitNullChecks(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return void createInner(bytecodetests.ImplicitNullChecks); Code: 0: new #26 // class bytecodetests/ImplicitNullChecks$Inner 3: dup 4: aload_1 5: dup 6: invokestatic #27 // Method java/util/Objects.requireNonNull:(Ljava/lang/Object;)Ljava/lang/Object; 9: pop 10: invokespecial #28 // Method bytecodetests/ImplicitNullChecks$Inner."<init>":(Lbytecodetests/ImplicitNullChecks;)V 13: pop 14: return void lambda(java.lang.Object); Code: 0: aload_1 1: dup 2: invokestatic #27 // Method java/util/Objects.requireNonNull:(Ljava/lang/Object;)Ljava/lang/Object; 5: pop 6: invokedynamic #29, 0 // InvokeDynamic #0:get:(Ljava/lang/Object;)Ljava/util/function/Supplier; 11: astore_2 12: return }
相关问答
更多-
在Java Lambda中,为什么getClass()在一个被捕获的变量上被调用(In Java Lambda's why is getClass() called on a captured variable)[2023-03-29]
是的,调用getClass()已经成为一个规范的“测试null ”成语,因为getClass()被认为是一个便宜的内在操作,我想HotSpot可能能够检测到这种模式并将操作减少到一个内在的null如果没有使用getClass()的结果,则检查操作。 另一个例子是用外部实例创建一个不是this的内部类实例: public class ImplicitNullChecks { class Inner {} void createInner(ImplicitNullChecks obj) { ... -
自Kotlin 1.1以来,最简单的方法是使用类参考语法 : something::class 如果您使用Kotlin 1.0,则可以通过调用.kotlin扩展属性将获得的Java类转换为KClass实例: something.javaClass.kotlin The easiest way to achieve this since Kotlin 1.1 is the class reference syntax: something::class If you use Kotlin 1.0, yo ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
10倍差异最可能的情况是JVM没有完全预热。 如果你不这样做,即使在Java中,你也可以看到超过10倍的性能差异。 我会尝试以10,000的批次运行并忽略前几次运行。 public static void main(String... args) throws IOException { timeObjectGraph("First run", 1); timeObjectGraph("Second run", 2); timeObjectGraph("Next thousand", ...
-
object.getClass()返回Class extends S> Class extends S> ,因此对象应满足该要求,因为它保证是S的子类。 这是正确的,但是你的第二个参数类型是Class
而不是Class extends S> Class extends S> ,不能隐式转换。 在您的示例中,编译器将S替换为用于调用函数的任何声明的类,但它可以是子类。 getClass()返回具体类。 例如,请考虑以下事项: Animal a = new Cat(); postJson(" ... -
调用Object.getClass()本身是否使用Reflection?(Does calling Object.getClass() itself use Reflection?)[2022-02-25]
检查Java反射定义的权威源代码, Java语言规范 ,第1.4节。 与预定义的类和接口的关系 ,只是说: 因此,本规范没有详细描述反射。 许多语言结构在Core Reflection API( java.lang.reflect )和Language Model API( javax.lang.model )中都有javax.lang.model ,但这些内容通常不在这里讨论。 Java虚拟机规范 ,第2.12节。 班级图书馆说: 可能需要来自Java虚拟机的特殊支持的类包括那些支持: 反射,例如包jav ... -
没有。 GWT不会模拟getPackage() : https : //developers.google.com/web-toolkit/doc/latest/RefJreEmulation 。 GWT的座右铭是在编译时而不是运行时执行最大化。 No. getPackage() is not emulated by GWT: https://developers.google.com/web-toolkit/doc/latest/RefJreEmulation. The motto of GWT is t ...
-
使用调试器步骤检查轨道是什么时。 它是空的吗? 如果发生异常,您应该看到异常。 您还可以将.getClass()包装在try catch中,如果您还没有使用调试器,则可以打印出任何异常。 When using the debugger step in to inspect what track is. Is it null? You should see an exception if it is occurring. You could also wrap your .getClass() in a tr ...
-
该变量未被重置,它是变量的不同副本。 事实上,有一堆的副本。 首先是你创建的lambda状态。 第二个是在构建第一个std::function创建的。 您必须记住它将它收到的可调用对象复制到自身中。 所以每一次tester调用都会启动一连串的副本。 解决这个问题的一种方法是在一个std::reference_wrapper传递lambda。 tester(std::ref(getNum)); tester(std::ref(getNum)); 引用包装将被复制,但所有副本将引用相同的lambda对象get ...
-
根据我的理解,C ++的dynamic_cast和Java的getClass()松散相关 C ++ typeid运算符可以提供更好的比较。 主要区别在于,在C ++中, typeid要求一个类至少有一个虚函数,而Java中的getClass可以在没有限制的任何类型的对象上调用。 返回类型(类)实际代表什么? 它表示一个适合在类上进行反射的Class对象 - 在C ++中没有直接对应的东西。 例如,您可以确定该类实现的所有接口,类的基类,列出所有方法,列出所有字段,按名称调用方法,等等。 From my un ...