This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

write(int c)  RSS feed

 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at the Java API the other day and I found something in the BufferedWriter class that I don't understand. It's this method.


write(int c)
Write a single character.


So, I suppose if you do something like myBufferedWriter.write(6); you can put a single character '6' on the end of your buffer. But what happens when you do something like myBufferedWriter.write(897); ?
What happens then? Do you get an IOException? Why doesn't the Java API explain this better?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you're passing the unicode value of the character.
myBufferedWriter.write(6) will not write '6', but will ouput a value called ACK in the unicode table.
myBufferedWriter.write('6') will output 6.

897 will output the character whose unicode is 0x0381, which does not exist.
 
Mandar Max
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write(6) does not write the character '6' to the outputstream, instead the int value is converted to byte by retaining the lower 8 bits. So when you call write(897) (897 ==> 1110000001 binary) the actual bits written are - 10000001.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mandar:
write(6) does not write the character '6' to the outputstream, instead the int value is converted to byte by retaining the lower 8 bits. So when you call write(897) (897 ==> 1110000001 binary) the actual bits written are - 10000001.


Sorry, but I'm pretty sure that's wrong. It does not convert the int to a byte, it converts it to a char. A char in Java has 16 bits and represents a Unicode character via UTF-16 encoding. Therefore, it does not strip the upper bits of 897 in the way you suggest.

The previous poster is correct.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? I have.
What happens? Look in the Unicode charts and see what 6 or 897 mean.
Does 897 actually exist? 6 certainly does, only you are better off calling it 0x0006 because the charts are denominated in hexadecimal.

CR
 
Mandar Max
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Oh it was for java.io.Writer...

Yes, the write(int) method of java.io.Writer writes the low order 16 bits of an integer.

What I was mentioning was java.io.OutputStream


public abstract void write(int b)
throws IOException

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored."


[ May 16, 2006: Message edited by: Mandar Max ]
[ May 16, 2006: Message edited by: Mandar Max ]
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Now, is there a method in java that can do the opposite operation, that is, change a single character to its Unicode number?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you cast a char to an int, or allow it to be promoted, it will be promoted to its UNICODE value. In the case of the simple alphabetic characters, this is the same as their ASCII values.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!