File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "PrintStream class" Watch "PrintStream class" New topic
Author

PrintStream class

Komal Amaresh
Ranch Hand

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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(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

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

Joined: Apr 22, 2009
Posts: 1
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

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.

HTH


SCJA
~Currently preparing for SCJP6
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: PrintStream class
 
Similar Threads
Compile time Difference between // and /* */
HttpServletResponse Output Stream
Passing Ping Command Through Telnet
diff between write and println function
accept input about patients, write it to a file, display the file's contents