感谢优快云 jack_chen
import java.io.*;
public class abc {
public static void main(String[] args) {
char cr = 1;
byte be = 1;
short st = 1;
int it = -1;
long lg = 1;
float ft = 1;
double de = 1;
int cnt;
byte x;
int cot = 0;
int byteSize = 0;
// System.out.println("Float.size: "+Float.SIZE);
// System.out.println("Double.size: "+Double.SIZE);
for (cnt = 1; (cr <<= 1) != 0; cnt++)
;
System.out.println("char'sBits:" + cnt);
for (cnt = 1; (be <<= 1) != 0; cnt++)
;
System.out.println("byte'sBits:" + cnt);
byteSize = cnt;
for (cnt = 1; (st <<= 1) != 0; cnt++)
;
System.out.println("short'sBits:" + cnt);
for (cnt = 1; (it <<= 1) != 0; cnt++)
;
System.out.println("int'sBits:" + cnt);
for (cnt = 1; (lg <<= 1) != 0; cnt++)
;
System.out.println("long'sBits:" + cnt);
// 用字节流处理double
try {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(buf);
out.writeDouble(de);
out.close();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
buf.toByteArray()));
while ((x = (byte) in.readByte()) != -1) {
// System.out.println("byte["+cot+"]:"+x);
cot++;
}
in.close();
} catch (IOException e) {
System.out.println("EOF!!!");
}
System.out.println("The Double's bit size is :" + cot * byteSize);
// 用字节流处理float
try {
cot = 0;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(buf);
out.writeFloat(ft);
out.close();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
buf.toByteArray()));
while ((x = (byte) in.readByte()) != -1) {
// System.out.println("byte["+cot+"]:"+x);
cot++;
}
in.close();
} catch (IOException e) {
System.out.println("EOF!!!");
}
System.out.println("The Float's bit size is :" + cot * byteSize);
}
}
import java.io.*;
public class abc {
public static void main(String[] args) {
char cr = 1;
byte be = 1;
short st = 1;
int it = -1;
long lg = 1;
float ft = 1;
double de = 1;
int cnt;
byte x;
int cot = 0;
int byteSize = 0;
// System.out.println("Float.size: "+Float.SIZE);
// System.out.println("Double.size: "+Double.SIZE);
for (cnt = 1; (cr <<= 1) != 0; cnt++)
;
System.out.println("char'sBits:" + cnt);
for (cnt = 1; (be <<= 1) != 0; cnt++)
;
System.out.println("byte'sBits:" + cnt);
byteSize = cnt;
for (cnt = 1; (st <<= 1) != 0; cnt++)
;
System.out.println("short'sBits:" + cnt);
for (cnt = 1; (it <<= 1) != 0; cnt++)
;
System.out.println("int'sBits:" + cnt);
for (cnt = 1; (lg <<= 1) != 0; cnt++)
;
System.out.println("long'sBits:" + cnt);
// 用字节流处理double
try {
ByteArrayOutputStream buf = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(buf);
out.writeDouble(de);
out.close();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
buf.toByteArray()));
while ((x = (byte) in.readByte()) != -1) {
// System.out.println("byte["+cot+"]:"+x);
cot++;
}
in.close();
} catch (IOException e) {
System.out.println("EOF!!!");
}
System.out.println("The Double's bit size is :" + cot * byteSize);
// 用字节流处理float
try {
cot = 0;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(buf);
out.writeFloat(ft);
out.close();
DataInputStream in = new DataInputStream(new ByteArrayInputStream(
buf.toByteArray()));
while ((x = (byte) in.readByte()) != -1) {
// System.out.println("byte["+cot+"]:"+x);
cot++;
}
in.close();
} catch (IOException e) {
System.out.println("EOF!!!");
}
System.out.println("The Float's bit size is :" + cot * byteSize);
}
}