Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte and character streams

 
michael delta
Ranch Hand
Posts: 35
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when we read a character stream it is always 2 bytes (transfering two by two or byte-byte?)
When we write in a binary stream,no data transformation occurs for example an int is written as 4byte(because that's what it is in ram), a character 2 bytes etc.
Also the integer 5 would be stored as 101. Why is 101 and not 00000000 00000000 00000000 00000101 as long as it commits 4 bytes per int?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15439
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a character does not always take up two bytes when read from or written to a stream. How many bytes a character uses depends on the encoding.

If you use ASCII, a character is always 1 byte. If you use UTF-8, then the number of bytes per character is variable (at least 1 and at most 4 bytes).

In Java I/O, there are two kinds of classes: streams and readers / writers. Streams (InputStream and OutputStream) are used for reading and writing binary data (i.e. you use them to read the exact contents of a file on disk, for example). Readers and writers are used to read or write text. Readers and writers convert the bytes from and to characters, using a character encoding.

An int in Java is always 32 bits in memory, regardless of which bits are set to 0 and which bits are set to 1.
 
michael delta
Ranch Hand
Posts: 35
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many bytes a character uses depends on the encoding.

So,java uses unicode(multibyte) but is able to manage ascii code(1 byte) also?Or always uses unicode and identifies the char size?

An int in Java is always 32 bits in memory, regardless of which bits are set to 0 and which bits are set to 1.

5 is 101,if i read an integer 5 i would read 3 bytes of zeros and a "byte of 5" right?

If i stop making these silly questions can i move on to I/O?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15439
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In memory, a char is a 16-bit Unicode character. But when you write that character to a file, it has to be converted to a representation, using a character encoding. ASCII is just one of the available character encodings. Ofcourse you can't encode all Unicode characters in ASCII - you'll lose information when you use ASCII.

michael delta wrote:5 is 101,if i read an integer 5 i would read 3 bytes of zeros and a "byte of 5" right?

An int is 32 bits = 4 bytes in memory, if the int would have the value 5 then that would be three bytes with a value of zero and one byte with a value 00000101 (in binary). But note that it's not as simple as that - the order in which those four bytes are stored can be different. Different kinds of processors store integers in a different order in memory, see endianness.
 
michael delta
Ranch Hand
Posts: 35
Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great,thank you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic