Serialization question

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Serialization question

Post by The_Nick »

What's the difference between ObjectOutputStream.writeUTF() and ObjectOutputStream.writeBytes()

Why would I choose one rather than the other?
I have read the API of each topic however I am still quite confused about it:
1) writeBytes() write 1 byte for each character non considering the higher bit.. ok
2) writeUTF() use UTF-8 encoding which uses 8 bits.. so it's not like this method uses UTF-16 and can encode more unicodes.. It still read 8 bits..
Furthermore writeBytes is to be used if the lenght of the String is not important. What does that mean? Real life example?
Furthermore, quoting the javadoc:
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
...
I would be glad if anybody could provide me with a clarification.
Thanks a lot in advance.

The_Nick.

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Serialization question

Post by admin »

The difference is in the bytes that are written to the stream. When you write a character to a stream as a byte, you write the actual byte representation of that character (i.e. 97 for a and so on), but when you write a character as UTF, it will be encoded in the UTF format which may or may not be same as the byte. So when you read a character written as UTF back as a byte it may not be same as the byte representation of that character.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: Serialization question

Post by The_Nick »

But why do both methods work out only 8 bits of the 16 available in a char?
What if I am writing a char which takes advantage of the whole available bytes(2)?

Thanks in advance.

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Serialization question

Post by admin »

Where did you see that OOS.writeUTF works on only 8 bits of a character?
If you like our products and services, please help us by posting your review here.

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Serialization question

Post by admin »

If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: Serialization question

Post by The_Nick »

Very cool indeed, so sometimes it takes also 3 bytes..
The last question:
Why is writeBytes() usable only if the lenght of the string i not important?

Thanks in advance.

The_Nick.

admin
Site Admin
Posts: 10067
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Serialization question

Post by admin »

The_Nick wrote:
admin wrote: The last question:
Why is writeBytes() usable only if the lenght of the string i not important?

Thanks in advance.

The_Nick.
I am not sure what that means. What context was that statement was made in?
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

Re: Serialization question

Post by The_Nick »

Basically the writeBytes does not store the lenght of the String.
However writeUTF does.

I have gathered it reading this http://stackoverflow.com/questions/1799 ... tfstring-s thread.

Why writeUTF stores the lenght of the String?

Thanks in advance.

The_Nick.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 212 guests