Object.intValue()的奇怪行为 [英] Strange behaviour with Object.intValue()

查看:70
本文介绍了Object.intValue()的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个问题而苦苦挣扎,我不明白为什么它不起作用.如何通过 double obj 传递变量并将其转换为 int ?
为什么它不能在顶部代码段中起作用,而在行下方的底部代码段中起作用?

I am struggling with a problem, which I can't understand why it doesn't work. How do I pass a variable through the double obj and convert to int?
Why does it not work in the top code snippet, but it works in the bottom code snippet below the line?

唯一的区别似乎是添加了一个额外的变量,该变量也被键入为 double ?

The only difference seems to be adding an extra variable, which is also typed as a double?

//Converting double to int using helper

//This doesn't work- gets error message
//Cannot invoke intValue() on the primitive type double

double doublehelpermethod = 123.65;
double doubleObj = new Double( doublehelpermethod);
System.out.println("The double value is: "+ doublehelpermethod.intValue());
//--------------------------------------------------------------------------
//but this works! Why?

Double d = new Double(123.65);
System.out.println("The double object is: "+ doubleObj);

推荐答案

double 是原始类型,而 Double 是常规Java类.您不能在原始类型上调用方法.但是, intValue()方法在 Double 上可用,如

The double is a primitive type, while the Double is a regular Java class. You cannot call a method on a primitive type. The intValue() method is however available on the Double, as shown in the javadoc

可以在此处

这篇关于Object.intValue()的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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