帮我看段代码? 大虾帮帮忙,郁闷了很久了

pinecone 2003-10-17 10:18:59
下面是java.io.DataInputStream的源代码的一部分
弄不明白里面的readFloat() 函数, 弄不明白它是在哪里处理浮点数中的小数点的?
它的参数怎么是是一个readInt()返回的四个字节整数啊
大虾快帮我看看,我郁闷了很久了。


package java.io;

public
class DataInputStream extends FilterInputStream implements DataInput {

public DataInputStream(InputStream in) {
super(in);
}

public final int read(byte b[]) throws IOException {
return in.read(b, 0, b.length);
}

public final int read(byte b[], int off, int len) throws IOException {
return in.read(b, off, len);
}

public final void readFully(byte b[]) throws IOException {
readFully(b, 0, b.length);
}

public final void readFully(byte b[], int off, int len) throws IOException {
if (len < 0)
throw new IndexOutOfBoundsException();
InputStream in = this.in;
int n = 0;
while (n < len) {
int count = in.read(b, off + n, len - n);
if (count < 0)
throw new EOFException();
n += count;
}
}

public final byte readByte() throws IOException {
int ch = in.read();
if (ch < 0)
throw new EOFException();
return (byte)(ch);
}

public final int readInt() throws IOException {
InputStream in = this.in;
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
}

public final float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
}

public final double readDouble() throws IOException {
return Double.longBitsToDouble(readLong());
}

....................

}
...全文
33 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
whywzf 2003-10-17
  • 打赏
  • 举报
回复
这个不是值上的相等,它先把int转成了byte[],再把byte[]转成了float,其转换机制在我上面写的函数里,值上是不等的!
pinecone 2003-10-17
  • 打赏
  • 举报
回复
一个整数怎么把它变成浮点数?

到底 bits 是不是一个整数? 如果不是一个整数的话,为什么有int这个修饰符?

那既然是整数,为什么要转化为浮点数? 浮点数的小数点在哪里?

不明白的说。
whywzf 2003-10-17
  • 打赏
  • 举报
回复
public static float intBitsToFloat(int bits)去看一下这个函数吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧