首页 \ 问答 \ 将double转换为Int,向下舍入(Convert double to Int, rounded down)

将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 to int 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
更新时间:2023-07-14 19:07

最满意答案

是的,调用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 for null” idiom, as getClass() 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 intrinsic null-check operation, if the result of getClass() 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 the java.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
}

相关问答

更多
  • 是的,调用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 ...
  • 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 Class ,因此对象应满足该要求,因为它保证是S的子类。 这是正确的,但是你的第二个参数类型是Class而不是Class Class ,不能隐式转换。 在您的示例中,编译器将S替换为用于调用函数的任何声明的类,但它可以是子类。 getClass()返回具体类。 例如,请考虑以下事项: Animal a = new Cat(); postJson(" ...
  • 检查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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)