Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PrintStream class

 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir,

I' am reading about the PrintStream class and unable to understand the following code


the code is declaring an int and passing a char to it. the write method as such takes the bytevalue, it is said. but a mere assignment of a char to an int decleration should return an error. besides new line assignment is a string. furthermore, the write() takes bytes generally and real value can be retained by casting it to char. I presumed it would return a garbage value at the most. what is the code doing and what's happening there.
Please explain.

regards,
Komal
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Single quotes (like this: 'c' ) in Java do not mean a String -- they mean a character constant. The type of a character constant is char, and char is just an unsigned 16-bit integer. '\n' is a special character constant that stands for the newline character; the Unicode value is 10 .

You can always assign a narrower integral value to a wider one without a cast (and without an error.) byte, short, char, int, and long are the five integral types in Java. So you can, for example, assign a char to an int, as the code does here. Because int is a wider type -- i.e., int has more bits than char -- the value is preserved exactly by the assignment. No garbage values.

Finally, OutputStreams have several overloaded "write()" methods; the one that is being used here accepts an int (which is cast to a byte before printing).
 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the write method only print characters. As it takes int and writes a byte value to the stream, why does it not write an int despite accepting an int as its parameter.
Furthermore unless a new line charater is passed to the write(), it does not return the character passed. how does new line enable auto flush.

b = 'c';
System.out.write(b);
//System.out.write('\n');//here it does not print c unless decommented

Secondly

int b = 10;
System.out.write(b);
System.out.write('\n');

Here nothing is printed on the console.

How and where should the write() be used
please explain

Thanks in advance,
Komal
 
Campbell Ritchie
Sheriff
Pie
Posts: 50206
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Komal Amaresh wrote:Does the write method only print characters. . . .
Have you read about it in the API documentation?
 
Przemek Matkowski
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Komal Amaresh wrote:
Secondly

int b = 10;
System.out.write(b);
System.out.write('\n');

Here nothing is printed on the console.

l


Hi,

Are You sure that there is nothing printed out? Please keep in mind that printing int value 10 you are printing 'non-visible' character ('\n').

Regards
Przemek
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should check out this story. It will really nail down the whole "what can go into what?" questions.

As for your other question about the actual write() method. Read up on the documentation (someone already provided) then change your code and play around. What happens if instead of "b=10" you do "b=65" or "b='W'"? Keep playing around until you can tell what the JVM is doing.

HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic