This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes reading chars and ints in bytes : confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "reading chars and ints in bytes : confusion" Watch "reading chars and ints in bytes : confusion" New topic
Author

reading chars and ints in bytes : confusion

rijagu chan
Greenhorn

Joined: Apr 10, 2000
Posts: 7
In a file how do you read chars and ints in terms of bytes:
for example :
String a = "abcd" is 4 bytes
int b = 12345 is 1 byte

i don't understand how. Could you please explain.
Thanks and Kudos to all of you.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I dont understand. How does int have one byte ? .
In java an int takes up 4 bytes and a character takes up 2 bytes.
rijagu chan
Greenhorn

Joined: Apr 10, 2000
Posts: 7
sorry for the confusion:
Lets write to a file the following:
WriteBoolean(false);
writeInt(123456);
writeInt(8670);
file.seek(5);
System.out.println(readInt());
answer is 8670
please explain
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In the file the information will be as follows
byte-0 byte-1 byte-2 byte-3 byte-4 byte5 byte6 byte7 byte8
false <------123456 ------------> <-----8670------------>
call to seek(5) will position the file pointer right before 8670, so that the readInt() returns 8670.
I am making any sense ?
- srini
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
WriteBoolean(false): writes 1 byte
writeInt(123456) : writes 4 Bytes
writeInt(8670) : writes 4 bytes
file.seek(5) : seeks to the 5th byte and since all read/write indexes start from 0 seek(5) points to the 6 position ie starting of 8670.
System.out.println(readInt()) : Reads 4 bytes and prints 8670.
Hope it's clear.
rijagu chan
Greenhorn

Joined: Apr 10, 2000
Posts: 7
Perfect. Clear and makes sense, thanks a lot.
 
wood burning stoves
 
subject: reading chars and ints in bytes : confusion