在 CSDN 改版前夕升至星星了(讨厌新版的样式),小小地庆祝一下。为了给大家技术分,所以要先回答问题(是很简单的题目啦),回答对了才有分哦~~我们都知道在计算机内部 int 和 float 都是采用 32 位二进制来表示的,它们在计算机内部表现形式是一致,也就是说一个 float 类型的数据根据内部的二进制位可以转换成 int 类型,反之亦是。当然了,long 和 double 也有这种关系的。完成以下 float2int、int2float 两个方法来实现这种转换。public class Test {
public static void main(String[] args) {
float f = 3.1415F;
int num = float2int(f);
float f1 = int2float(num);
System.out.println(num); // 输出 1078529622
System.out.println(f1); // 输出 3.1415
} public static int float2int(float f) {
}
public static float int2float(int num) { }
}
public static void main(String[] args) {
float f = 3.1415F;
int num = float2int(f);
float f1 = int2float(num);
System.out.println(num); // 输出 1078529622
System.out.println(f1); // 输出 3.1415
} public static int float2int(float f) {
}
public static float int2float(int num) { }
}
public static int float2int(float f) {
return new Float(f).hashCode();
}
public static float int2float(int num) {
return Float.intBitsToFloat(num);
}
floatToIntBits
intBitsToFloat
equals和hashCode,如果重写equals方法,那么hashCode方法也必须要重写,LZ可能要阐述这方面的东西吧。
自己写一个也是几分钟的事,繁简不是以代码多少做为标准的,你调用了别人写的一个函数一下搞定了就算简单了?
return Float.floatToIntBits(f);
}