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.
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).
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('\n');//here it does not print c unless decommented
int b = 10;
Here nothing is printed on the console.
How and where should the write() be used
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.