import java.io.*;

class file2
{
	public static void main(String args[]) throws IOException
	{
		

		int b;

		b='A';

		System.out.write(b);
		System.out.write('\n');


	}
}

will anyone can tell that when i don't write System.out.write('\n'); then nothing comes in output. but after adding this statement 'A' got printed, why is it so ? secondly, i have changed this 'A' to int, then why even after that 'A' is printed. 65 must be there. why ?

1. Surprisingly, it seems that output to System.out doesn't get sent to the console until the line is terminated - I reproduced this behaviour in Eclipse as well, but I'm sure that's not always so.
2. Please see my latest post re wait/notify. If you have a quick look at the API doc for PrintStream (System.out is a PrintStream, as the API doc for System will confirm) you will find your answer in the doc for its write(int b) method

ps: I look a bit further in the PrintStream API doc and found that when you call write(int b) the data is buffered, and is only written to the uderlying stream if it has automatic flush enabled, or when you call the flush() method.

System.out.write(b);
System.out.flush();

Obviously System.out doesn't have autoflush enabled.

1. Surprisingly, it seems that output to System.out doesn't get sent to the console until the line is terminated - I reproduced this behaviour in Eclipse as well, but I'm sure that's not always so.
2. Please see my latest post re wait/notify. If you have a quick look at the API doc for PrintStream (System.out is a PrintStream, as the API doc for System will confirm) you will find your answer in the doc for its write(int b) method

so whats the answer to first question ? why is it so ? why is it neccessary to terminate the line ??

1. Surprisingly, it seems that output to System.out doesn't get sent to the console until the line is terminated - I reproduced this behaviour in Eclipse as well, but I'm sure that's not always so.
2. Please see my latest post re wait/notify. If you have a quick look at the API doc for PrintStream (System.out is a PrintStream, as the API doc for System will confirm) you will find your answer in the doc for its write(int b) method

ps: I look a bit further in the PrintStream API doc and found that when you call write(int b) the data is buffered, and is only written to the uderlying stream if it has automatic flush enabled, or when you call the flush() method.

System.out.write(b);
System.out.flush();

Obviously System.out doesn't have autoflush enabled.

public void write(int b)
throws IOException
Writes the specified byte to this output stream.
The write method of FilterOutputStream calls the write method of its underlying output stream, that is, it performs out.write(b).

Implements the abstract write method of OutputStream.

Specified by:
write in class OutputStream
Parameters:
b - the byte.
Throws:
IOException - if an I/O error occurs.

this is API. tell me what should i refer for answer ?

The write method of FilterOutputStream ...

System.out is a PrintStream. Here's what the API for PrintStream says

public void write(int b)

Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

Online latest API doc is at:
http://docs.oracle.com/javase/7/docs/api/

System.out is a PrintStream. Here's what the API for PrintStream says


Online latest API doc is at:
http://docs.oracle.com/javase/7/docs/api/

ya ya ! i have already seen it. but i have learnt many good things from API. thanks for telling this. but can u explain that y it is changing it in char ? 'A' is printed but i want 65 to be printed.:'(

Like is says in the doc...
write(int b) writes the byte (decimal 65) to the stream. The console interprets all data as ASCII/UniCode characters, so that byte is displayed as an A.
If you want to display the int as an integer valaue converted to a String in the usual way for your PC's locale etc, use print(int i) rather than write(int b)

Like is says in the doc...
write(int b) writes the byte (decimal 65) to the stream. The console interprets all data as ASCII/UniCode characters, so that byte is displayed as an A.
If you want to display the int as an integer valaue converted to a String in the usual way for your PC's locale etc, use print(int i) rather than write(int b)

means i can't print integer i m giving with the help of write ? :-0 hey! why in the case of write only it is happening ? when i use println, then it prints exaclty that what i want. but y write not ?

Because write is for writing raw bytes of binary data, and print is for printing things in human-readable form.

Because write is for writing raw bytes of binary data, and print is for printing things in human-readable form.

ohh! any one example which can tell write's very best use? small example only. just to learn. "raw bytes of binary data" actually i wana find this line meaning

Here's an example. In my HiFi I have a small remote control interface that accepts commands over an ethernet connection*. For example to set the volume to any level between 0 and 255 I send a two byte command. The first byte is decimal 86 (hex 56) which is the character 'V' in ASCII. The second byte is just the volume, 0-255 decimal (00 - FF hex). I do this with something like

int byte1 = 86, byte2 = 99; // volume 99/255
rc.write(byte1);  rc.write(byte2);

If I used print(byte2) then that would write two bytes 57,57 (hex 39,39) which is ASCII for "99"

* yes, really, I didn't make this up!

Here's an example. In my HiFi I have a small remote control interface that accepts commands over an ethernet connection*. For example to set the volume to any level between 0 and 255 I send a two byte command. The first byte is decimal 86 (hex 56) which is the character 'V' in ASCII. The second byte is just the volume, 0-255 decimal (00 - FF hex). I do this with something like

int byte1 = 86, byte2 = 99; // volume 99/255
rc.write(byte1);  rc.write(byte2);

If I used print(byte2) then that would write two bytes 57,57 (hex 39,39) which is ASCII for "99"

* yes, really, I didn't make this up!

i didn't get exactly. sorry to say. please any simpler one

Can't think of a simpler eg at the moment. If I think of one later I'll post it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.