Java:double vs float [英] Java: double vs float
本文介绍了Java:double vs float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在另一个Bruce Eckel练习中,我编写的代码在另一个类中接受一个方法并更改值。这是我的代码:
class Big {
float b;
}
public class PassObject {
static void f(Letter y){
y.c ='z';
} // end f()
static void g(Big z){
z.b = 2.2;
}
public static void main(String [] args){
Big t = new Big();
t.b = 5.6;
System.out.println(1:t.b:+ t.b);
g(x);
System.out.println(2:t.b:+ t.b);
} // end main
} // end class
PassObject.java:13:可能丢失精确度
发现:double
必需:float zb = 2.2
passobject.java:20:可能损失精度
发现:double
必需:float tb = 5.6
不能 double
s float $ c
解决方案
是,但您必须指定它们是浮动广告,否则将其视为双字:
zb = 2.2f
数字末尾的'f'表示浮点数而不是双精度。
Java不会自动将double减少为float。
In another Bruce Eckel exercise, the code I've written takes a method and changes value in another class. Here is my code:
class Big {
float b;
}
public class PassObject {
static void f(Letter y) {
y.c = 'z';
} //end f()
static void g(Big z) {
z.b = 2.2;
}
public static void main(String[] args ) {
Big t = new Big();
t.b = 5.6;
System.out.println("1: t.b : " + t.b);
g(x);
System.out.println("2: t.b: " + t.b);
} //end main
}//end class
It's throwing an error saying "Possible loss of precision."
PassObject.java:13: possible loss of precision
found: double
required : float z.b = 2.2
passobject.java:20: possible loss of precision
found : double
required : float t.b = 5.6
Can't double
s be float
s as well?
thanks in advance
解决方案
Yes, but you have to specify that they are floats, otherwise they are treated as doubles:
z.b = 2.2f
The 'f' at the end of the number makes it a float instead of a double.
Java won't automatically narrow a double to a float.
这篇关于Java:double vs float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文