aspose file tools*
The moose likes Beginning Java and the fly likes PrintStream class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PrintStream class" Watch "PrintStream class" New topic

PrintStream class

Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67

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.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24168

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).

[Jess in Action][AskingGoodQuestions]
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
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
please explain

Thanks in advance,
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36579
Komal Amaresh wrote:Does the write method only print characters. . . .
Have you read about it in the API documentation?
Przemek Matkowski

Joined: Apr 22, 2009
Posts: 1
Komal Amaresh wrote:

int b = 10;

Here nothing is printed on the console.



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').

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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.


~Currently preparing for SCJP6
I agree. Here's the link:
subject: PrintStream class
Similar Threads
diff between write and println function
Compile time Difference between // and /* */
HttpServletResponse Output Stream
accept input about patients, write it to a file, display the file's contents
Passing Ping Command Through Telnet