Steve Cordrey
Steve Cordrey

Reputation: 53

Java integer is equal to character?

I apologize if this question is a bit simplistic, but I'm somewhat puzzled as to why my professor has made the following the statement:

Notice that read() returns an integer value. Using an int as a return type allows read() to use -1 to indicate that it has reached the end of the stream. You will recall from your introduction to Java that an int is equal to a char which makes the use of the -1 convenient.

The professor was referencing the following sample code:

public class CopyBytes {
   public static void main(String[] args) throws IOException {

    FileInputStream in = null;
    FileOutputStream out = null;

    try {
        in = new FileInputStream("Independence.txt");
        out = new FileOutputStream("Independence.txt");
        int c;

        while ((c = in.read()) != -1) {
            out.write(c);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
 }
}

This is an advanced Java course, so obviously I've taken a few introductory courses prior to this one. Maybe I'm just having a "blonde moment" of sorts, but I'm not understanding in what context an integer could be equal to a character when making comparisons. The instance method read() returns an integer value when it comes to EOF. That I understand perfectly.

Can anyone shed light on the statement in bold?

Upvotes: 4

Views: 9039

Answers (6)

David Allan Houser Jr
David Allan Houser Jr

Reputation: 127

I am not sure what the professor means but what it all comes down to is computers only understand 1's and 0's we don't understand 1's and 0's all that we'll so we use a code system first Morris code then ascii now utf -16 ... It varies from computer to computer how accurate numbers(int) is.you know in the real world int is infinate they just keep counting.char also has a size.in utf _16 let's just say it's 16 bits (I will let you read up on that) so if char and int both take 16 bits as the professor says they are the same (size) and reading 1 char is the same as 1int . By the way to be politically correct char is infinite as well.Chinese characters French characters and the character I just made up but can't post cause its not supported.so think of the code system for int and char. -1 int is eof char.(eof = end of file) good luck, I hope this helped.what I don't understand is reading and writing to the same file?

Upvotes: 0

Justin
Justin

Reputation: 4216

The char data type in Java is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

The int data type in Java is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).

Since char cannot be negative (a number between 0 and 65,535) and an int can be negative, the possible values returned from the method is -1 (to signify nothing left) to 65,535 (max value of a char).

Upvotes: 1

ajb
ajb

Reputation: 31699

It means your professor has been spending too much time programming in C. The definition of read for InputStream (and FileInputStream) is:

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

(See http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read())

A char in Java, on the other hand, represents a Unicode character, and is treated as an integer in the range 0 to 65535. (In C, a char is an 8-bit integral value, either 0 to 255 or -128 to 127.)

Please note that in Java, a byte is actually an integer in the range -128 to 127; but the definition of read has been specified to avoid the problem, by decreeing that it will return 0 to 255 anyway. The javadoc is using "byte" in a loose sense here.

Upvotes: 1

Brandon
Brandon

Reputation: 10058

What your professor is referring to the fact that characters are just integers used in a special context. If we ignore Unicode and other encoding types and focus on the old days of ASCII, there was an ASCII table (http://www.asciitable.com/). A string of characters is really just a sequence of integers, for example, TUV would be 84 followed by 85 followed by 86.

The 'char' type is an integer internally in the JVM and is more or less a hint that this integer should only be used in a character context.

You can even cast between them.

char a = (char) 65;
int i = (int) 'A';

Those two variables hold the same data in memory, but the compiler and JVM treat them slightly differently.

Because of this, read() returns an integer instead of char so as to allow a -1, which is not a valid character code. Values other than -1 can be cast to a char, while -1 indicates EOF.

Of course, Unicode changes all of this with multi-byte character and code points. I'll leave that as an exercise to you.

Upvotes: 0

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

In Java, chars is a more specific type of int. I can write.

char c = 65;

This code prints out "A". I need the cast there so Java knows I want the character representation and not the integer one.

public static void main(String... str) {
    System.out.println((char) 65);
}

You can look up the int to character mapping in an ASCII table.

And per your teacher, int allows for more values. Since -1 isn't a character value, it can serve as a flag value.

Upvotes: 5

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

To a computer a character is just a number (that may at some point be mapped to a picture of a letter for display to the user). Languages usually have a special character type to distinguish between "just a number" and "a number that refers to a character", but inside, it's still just some sort of integer.

The reason why read() returns an int is to have "one extra value" to represent EOF. All the values of char are already defined to mean something else, so it uses a larger type to get more values.

Upvotes: 1

Related Questions