Java Reference
In-Depth Information
codingavalueintherange-128to127.Themaximumpositivevaluefora
byteoperandis127.Therefore,thevalues128through255arenotlegal.
Programmersnote:
Two'scomplementrepresentationsareanencodingschemeforsigned
binaryintegersdesignedtofacilitatemachinearithmetic.Thevalueof
thetwo'scomplementisthedifferencebetweenthenumberandthe
nextintegerpoweroftwothatisgreaterthanthenumber.Asimpleway
ofcalculatingthetwo'scomplementofabinarynumberisnegatingall
thedigitsandaddingonetotheresult.Anadditionaladvantageoftwo's
complementrepresentationsisthatthereisnoencodingfornegative
zero.
OneofthedifficultiesofJavastreamoperationsisthatthebytedata
typeisnotconvenient.Whilemanyofthemethodsinthestreamclasses
aredocumentedtoacceptorreturnbytearguments,inreality,theyoperate
onintdata.ThemainreasonisthatthereisnobyteliteralsinJava,al-
thoughthecompilersometimesmakesautomaticassignmentconversions;
forexample:
byte val1 = 22;
// Valid assignment
byte val2 = 44;
// Valid assignment
however,
byte val3 = val1 + val2; // Illegal. Requires type cast
byte val4=1=3;
//Illegal. Requires type cast
Thesmallrangeofthebytedatatypeexplainswhytheyareoftencon-
vertedtointincalculations.Laterthecalculatedvaluesaretypecastback
intothebyteformat.Thismeansthatalthoughastreamisdefinedtooper-
ateonbytedata,internalprocessingofnumericdatabystring-based
classesisoftendoneonintdatatypes.
JavaInputStreamclass
TheInputStreamclass,locatedinthejava.iopackage,istheabstractclasson
whichallinputstreamsarebased.Theclasscontainsseveralmethodsasso-
ciatedwithinputstreams,includingreadingdatafromthestream,closing
andflushingstreams,andcheckinghowmanybytesofdataareavailable. Ta-
ble18-2 , onthefollowingpage,liststhemethodsoftheInputStreamclass.
Themethodread()isdesignedtoobtainbytedatafromtheinput
stream.In Table18-2 youcanseeread()isoverloadedinthreedifferentim-
plementations:read(),read(byte[]b),andread(byte[]b,intoff,intlen).
Search WWH ::




Custom Search