+ 2
I want to know use of System.in.read(). for what purpose it is used. In the fillowimg program output is a=65535 . why?
class Program { public static void main(String[ ] args) { try{ int a; a=(char)System.in.read(); System.out.println("a="+a); } catch (Exception b) { System.out.println""("exception caught "); } } }
2 Antworten
+ 3
Don't cast to char; use int. You're getting a signed integer back, value -1 (byte becomes:65536-1=65535), which indicates end of stream.
Also, read this answer to explain what it does (I'm not going to summarize because anyone can Google this/similar):
http://stackoverflow.com/a/34120618/3981745
Quote relevant to you:
"It returns an int because besides all the possible values of a byte, it also needs to be able to return an extra value to indicate end-of-stream. So, it has to return a type which can express more values than a byte can."
+ 1
@kirk thanks