wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes KM question  (write method) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "KM question  (write method)" Watch "KM question  (write method)" New topic
Author

KM question (write method)

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
this one is from KM exercise 18.7
If write(0x01234567) is called on an instance of OutputStream, what will be written to the destination of the stream ?
answer is 0x67
why ?
api says for write write(int v) ==> writes the specified byte to this output stream.
what do we deduce from this ? basically the number 0x01234567 is a hexadecimal. right ? so how come only last two digits were written out ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

api says for write write(int v) ==> writes the specified byte to this output stream.

API is right, write(int v) writes a byte to the stream... You have to provide an int but only the lowest byte is written... I remember a previous discussion on this but I can't find the page again. Maybe if you do a search (kw: byte write), you should find it.
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Are you sure it is OutputStream. The JDK documentation says OutputStream is an abstract class?
Can you check what class the question refers to ?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
the method write(int v) is declared (abstract) in class OutputStream and is implemented by subclasses of InputStream. It doesn't matter where it is implemented, the bottom line is that the contract of the method MUST be honored by implementing classes. Moreover, the write(int v) method is implemented natively as far as I know
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
out of this number how does one get the lowest byte ??? (meaning the last 8 bits...)
Originally posted by Valentin Crettaz:

API is right, write(int v) writes a byte to the stream... You have to provide an int but only the lowest byte is written... I remember a previous discussion on this but I can't find the page again. Maybe if you do a search (kw: byte write), you should find it.
HIH
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
a cast to byte does the job...
int i = 0x01234567;
byte b = (byte)i;
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Originally posted by mark stone:
this one is from KM exercise 18.7
If write(0x01234567) is called on an instance of OutputStream, what will be written to the destination of the stream ?
answer is 0x67
why ?
api says for write write(int v) ==> writes the specified byte to this output stream.
what do we deduce from this ? basically the number 0x01234567 is a hexadecimal. right ? so how come only last two digits were written out ?


Actually it is not the last 67 its written. This is how it works
what write(int i ) does
The first write() method takes an int as argument,
but truncates it down to the eight least significant bits before writing it out as
a byte.
even the write(0x1234567) does the same
The binary value of 0x1234567 is
1001000110100010101100111
it truncate the above binary code to 8 least significant bit which results in
01100111
This value in hexadecimal is 0x61.
This is what it does. Hope it helps. Even if i am wrong please correct me


Arathi<br />Sun Certified Java Programmer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
How come 01100111 is 0x61 ???
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
i am confused now. also there would be no calculator or JVM where one can run a quick program.
please let me know so that i can go down to paper and pencil.
How to extract the last byte from this hex number ? sounds simple. but actually it does not come out that way.
Originally posted by Valentin Crettaz:
How come 01100111 is 0x61 ???
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Originally posted by Valentin Crettaz:
How come 01100111 is 0x61 ???

sorry its 0x67.
Typing mistake
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
ok now to sum up
0x01234567 is an int but only the low-level byte will be written, which is 0x67.
So Arathi what was the point of your post in which you said that it is not 67 that will be written?
[ January 15, 2002: Message edited by: Valentin Crettaz ]
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
valentin,
precisely this was the point of my post. that how do and why do you take the last two numbers or rather digits to represent the "low-level byte".
this is the question. i hope there is no offense taken here.

Originally posted by Valentin Crettaz:
ok now to sum up
0x01234567 is an int but only the low-level byte will be written, which is 0x67.
So Arathi what was the point of your post in which you said that it is not 67 that will be written?
[ January 15, 2002: Message edited by: Valentin Crettaz ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Valentin Crettaz,
if the value is given in hexadecimal, then YES, you can just take the low order byte, which will be the last two characters...
0x01234567 - > low order byte is Ox67
This works in HEX.
Note that if it were a decimal number, like
1234567, you couldn't do this.
Rob


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Rob,
I agree with you, but you must admit that the question has been asked using a hexadecimal value. Of course it doesn't work the same way with decimal or octal. Sorry I should have made myself clear by emphasizing that point...
Mark,
an integer is made of 4 bytes. The high-level byte is on the left and the low-level byte on the right (little endian), that is, an int is composed of byte3,byte2,byte1,byte0. Thus, when you cast an int to a byte only byte0 remains.
HIH
Shivaji Marathe
Ranch Hand

Joined: Jan 11, 2002
Posts: 203
Arathi & Mark
You can always use Integer.byteValue() , Integer.toHexString(), Integer.toOctoalString(), and Integer.toBinaryString() methods to convert numbers among different formats.
HTH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KM question (write method)
 
Similar Threads
IO Quetion
OutputStream question
Output Stream
write(0x01234567)
Answer please