开发者

Why doesn't StringReader.Read() return a byte?

I was using StringReader in a Data Structures assignment (Huffman codes), and was testing if the end of the string had been reached. I found that the int value that StringReader.read() returns is not -1, but 65535, so casting the result to a byte solved my infinite loop problem I was having.

Is this a bug in JDK, or is it common practice to cast values 开发者_JAVA百科returned from Reader.read() calls to bytes? Or am I missing something?

The gist of my code was something like this:

StringReader sr = new StringReader("This is a test string");
char c;
do {
    c = sr.read();
//} while (c != -1);     //<--Broken
} while ((byte)c != -1); //<--Works


In fact that doesn't even compile. I get:

Type mismatch: cannot convert from int to char

Since the sr.read() call returns an int I suggest you store it as such.

This compiles (and works as expected):

StringReader sr = new StringReader("This is a test string");
int i;               // <-- changed from char
do {
    i = sr.read();

    // ... and if you need a char...
    char c = (char) i;

} while (i != -1);   // <-- works :-)

Why doesn't StringReader.Read() return a byte?

Strings are composed of 16-bit unicode characters. These won't fit in an 8-bit byte. One could argue that a char would have been enough, but then there is no room for providing an indication that the EOF is reached.


Characters in java are 2 bytes because they're encoded in UTF-16. This is why read() returns an int, because byte is not large enough.


 char c = (char) -1;
        System.out.println(""+c);
        System.out.println(""+(byte)c);

This code will solve your doubt ..


A Java String is a sequence of chars which are not bytes but values that represent UTF-16 code-points. The semantics of read is to return the next atom from the input stream. In case of a StringReader the atomic component is a 16-bit value which cannot be represented as a single byte.


StringReader#read returns an int value which is -1 if the end of the stream has been reached.

The problem in your code is that you already convert the int value to a char and test the char:

System.out.println("Is it still (-1)?: " + (int) ((char) -1));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜