我掙扎着一個問題,我不明白爲什麼它不起作用。如何通過double obj
傳遞變量並將其轉換爲int
?
爲什麼它不能在最上面的代碼片段中工作,但它可以在行下面的底部代碼片段中工作?奇怪的行爲Object.intValue()
唯一的區別似乎是添加一個額外的變量,它也被輸入爲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);
只是作爲一個有點挑剔挑剔這裏,但你的變量名的命名應該是一致的。如果你打算使用駱駝外殼作爲變量名稱,那麼可以通過它。混合搭配是一個糟糕的主意。此外,將「helpermethod」這個詞添加到變量名中可能會導致以後出現混淆。 – 2011-12-25 22:24:12