• Post Reply Bookmark Topic Watch Topic
  • New Topic

why int as return type in read() not char  RSS feed

 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public int read() throws IOException;

read() in java.io.Reader returns int, and to print it we have to cast it to char.

why not this method can have return type char instead of int.

one point can be it returns -1 in case no char exist thats why it returns int, but that can not be a sole reason as it leads to cast an output every time we read a char. may be million times in some big file.

Please assist.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you'll just have to ask it to James Gosling.
 
Matthew Walton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At a guess I'd say it's because you're not necessarily reading text. Generic file reading methods need to be able to cope elegantly with binary data as well. They can't use byte, because it doesn't have any values they could use to indicate read failure short of throwing an exception (not a good way to cope with end-of-file conditions which are not at all exceptional). That leaves int as a candidate with sufficiently wide coverage. The part of my brain which deals with more exotic languages suggests what you really want is a byte value which evaluates to false in boolean context if there's a read error, but you can't do that in Java, so you're left with either int or using byte and throwing an exception when you hit the end of the file. Ow. You also can't do an overload with int and one with char, because the parameter lists aren't distinct and Java can't resolve overloads solely based on return type (and neither can virtually every other language going).

If you're casting every single byte you read to a char, perhaps you should be looking at some of the other file reading methods available - perhaps BufferedReader.readLine() - which returns a String.
 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew
There is nothing more than char or beyond char we need to print.
We would always print char, not its ASCII value,
and as a genral rule, there should be a minimum scope where ever possible.
so char would be large enough, nothing more than char scoe needed..
than why return type as a char not int..

sir am not looking for alternatives, but just wanna know there must be some good logic behind that..

please assist.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parmeet,

Yes, it's "int" rather than "char" just so that a value that's not a char can be returned to signal end-of-file. The int value "-1" is different from any char value, so it's used for this purpose.



Note that InputStream.read() returns an int for the same reason. This practice was inherited from the C language; functions like fread() work the same way.

The cast from "int" to "char" is free; the JVM has no native "char" type, and all computations on char are actually done on ints instead. No JVM instructions are generated by the compiler for that cast.
[ April 03, 2008: Message edited by: Ernest Friedman-Hill ]
 
Parmeet Singh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. it was really helpful
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!