java Java中字节数据类型的实际使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12634177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Practical use of byte data type in Java
提问by Aayush
Besides the fact that bytes saves memory by consuming only eight bits of storage as against 32bitsfor integer
. What other practical uses does it serve? I read in a text that it is useful when we are working with stream of data from a network or a file. They are also useful when you're working with raw binary data that may not be directly compatible with Java's other built-in types. Could any one explain these with examples? and state a few more practical uses?
除此之外字节通过消耗只有八个存储位作为对节约了内存的事实,32位的integer
。它还有哪些实际用途?我在一篇文章中读到,当我们处理来自网络或文件的数据流时,它很有用。当您处理可能与 Java 的其他内置类型不直接兼容的原始二进制数据时,它们也很有用。任何人都可以用例子来解释这些吗?并说明一些更实际的用途?
采纳答案by Mukul Goel
As u read, bytes are useful when reading stream of bits
当您阅读时,字节在读取位流时很有用
Before telling the reason, lemme ask you a question how many bits or bytes a character is represented as ?? 8bits/1byte. I hope here you got the reason of byte.
在说原因之前,让我问你一个问题,一个字符表示多少位或字节??8 位/1 字节。我希望在这里你得到了字节的原因。
The reason of using byte when reading stream of bits is that when u read the stream in a byte, each time u read, u will have one byte of data in your byte type variable. I.e. 1 character. Thus while reading you will get a character at a time.
在读取位流时使用字节的原因是,当您以字节形式读取流时,每次读取时,您的字节类型变量中都会有一个字节的数据。即 1 个字符。因此,在阅读时,您将一次获得一个字符。
Also machines understand bits, so byte come in handy there as well, when reading from any input like keyboard,file,data stream etc we prefer byte. Similarly when writing to devices monitors , output stream, files , etc byte comes in handy.
机器也理解位,所以字节在那里也派上用场,当从键盘、文件、数据流等任何输入读取时,我们更喜欢字节。类似地,当写入设备监视器、输出流、文件等时,字节会派上用场。
also everything around is multiples of 10100010 , so when you are not sure what to expect from sender or what the receiver expects, use byte.
此外,周围的一切都是 10100010 的倍数,所以当您不确定发送方的期望或接收方的期望时,请使用字节。
回答by npinti
Usually byte arrays are used for serialization (to disk, network stream, memory stream, etc). A simple example of this could be something like so (taken from here):
通常字节数组用于序列化(到磁盘、网络流、内存流等)。一个简单的例子可能是这样的(取自这里):
Object object = new javax.swing.JButton("push me");
try {
// Serialize to a file
ObjectOutput out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
out.writeObject(object);
out.close();
// Serialize to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(object);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
} catch (IOException e) {
}
Another usage of the byte datatype is also related to images. For instance you could do something like so: byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
(taken from here) to access pixel related information.
字节数据类型的另一种用法也与图像有关。例如,您可以执行以下操作byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
:(取自此处)以访问像素相关信息。
回答by Arun Kumar
byte
is a 8-bit signed primitive-type in java. It is usefull in the situations where we are dealing with the data in the form of bytes such as "Reading/writing the byte data from/to files".
You can find the best practical example at here.
byte
是 java 中的 8 位有符号原始类型。它在我们以字节形式处理数据的情况下很有用,例如“从/向文件读取/写入字节数据”。您可以在此处找到最佳实践示例。