A good answer might be:

In order to return values that can't be represented in data type byte.

Reading until EOF

An unsigned integer is zero or positive. In Java (regardless of computer platform) the primitive type byte holds an integer in the range -128 to +127. An unsigned byte holds values 0 to +255, so something larger than datatype byte is needed. Data type int is returned because it is used more often than the other integer types.

Most methods of DataInputStream throw an EOFException when the end of a file is reached. Let us work on a program that uses this idea to read 32-bit integers until end of file:

  while ( true )
    sum += instr.readInt();

catch ( EOFException  eof )
  System.out.println( "The sum is: " + sum );

The while loop keeps going until readInt() hits the end of the file. Then an exception it thrown and execution jumps out of the loop to the catch{} block.


Will catch ( EOFException eof ) catch an IOException that is not an EOFException?