tags:

views:

161

answers:

3

and i see the character are only ASCII then its not dynamic right?

any explanation for this?

other question, what is the difference between byte streams and character streams?

+14  A: 

Bytes are not characters.

Let me repeat that: bytes are not characters.

A certain number of bytes, when decoded, can represent a character. For some encodings (like ASCII or ISO-8859-1), that certain number can be 1. But you should always be using an encoding of some sort to convert bytes to characters and vice versa, as there's not even any agreement on what char a byte might decode to.

The difference between a byte stream and a character stream is that the character stream works with characters and not bytes. If it's wrapped around a byte stream, the character stream has an encoding, and uses it to convert bytes from the underlying byte stream to characters (or of course, to convert characters written to the stream to bytes).

cHao
Why it has to be byte? not an int?
Keenan Gebze
Because underneath it all, almost all streams read and write data in byte-sized chunks. InputStreamReaders and OutputStreamWriters and DataOutputStreams and DataInputStreams and such as that can provide the illusion of writing data in bigger chunks than that, but even they write bytes behind the scenes in most cases (unless you have something like a StringWriter, that writes to a StringBuilder or something rather than an underlying stream). Those other streams just provide a way to convert the bytes into more usable data, like chars or ints.
cHao
ooh so the character streams only 'chungking' the byte streams. And is the readInt, readLong, read... function is also 'chungking'?
Keenan Gebze
Pretty much. They read a certain number of bytes and build them into an int, long, etc, much like a char stream would do for a char. The difference is that Java specifies how bytes are turned into ints and such, because there's only one or two ways anyway and Java chose one ("big-endian", for future reference) rather than having IntEncodings or something and complicating the process. They couldn't do that with chars, cause there are dozens of different (and common!) ways to translate bytes to chars, depending on the language and a few other things. So you have to specify an encoding there.
cHao
+2  A: 
Leo Holanda
I would be curious about what is printed.
mkoryak
Note: some wide characters are 32-bit. String supports UTF-16 encoding of these characters.
Peter Lawrey
Hi mkoryak. I edited the post two answer your curiosity
Leo Holanda
+1  A: 

In C and C++, a char holds a single byte, and the types charis used to mean an 8-bit integer, as well as a single character of text. Java is not like that.

In Java, a char and a byte are different data types. A char holds a single Unicode character which is (generally) larger than a byte. A byte holds an 8-bit integer. When you convert a char (or char[] or a String) to a byte array (type byte[]), the string is encoded according to some character encoding (usually UTF-8), and the result is how that particular string would be stored in memory (or on disk) if it was written according to that character encoding.

Java IO supports reading byte arrays (byte[]) directly to or from disk because this is how one generally works with binary files (i.e. non-text files, where linebreaks shouldn't be converted, and strings shouldn't be re-encoded). The bytes in that file may correspond to characters in an 8-bit encoding (like ASCII or ISO8859-*), but if you're going to use them that way, you should do an explicit conversion to a char[] or a String).

Ken Bloom