Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Q: Why in the write method for the OutputStream class we have "int b"?

JavaFAQ Home » Daily Tips Go to all tips in Daily Tips

Bookmark and Share

Question: Why in the write method for the OutputStream class we have "int b"? Was not it possible to have public abstract void write(byte b)?

I am confused: In javadoc for public abstract class OutputStream:

"public abstract void write(int b) throws IOExceptionWrites the specified byte to this output stream."

In the same description later: "The 24 high-order bits of b are ignored" ...? Why do we need to use int and ignore 3/4 of that afterwards...

Has it relation to memory management efficiency on 32-bits CPUs and OSs?

What's about 64-bit CPUs like Itanium... Will SUN change int to 64-bits size?

Or does SUN believe that in future bytes will be 32 bits long? Smile

Answer: Equivalent method read() returns an int also. This is to enable a return value of -1 in which is not possible if use byte.

Byte is signed and it has the value range: -128 to 127. And even if the sign is not so important there will not be any possibility to distinguish between -1 and 255!

And last point: byte can not be used directly for mathematical operations, it has to be converted to int anyway.

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy