Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

diff between write and println function  RSS feed

 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the program below:

//Demonstrate System.out.write().
class WriteDemo
{
public static void main(String args[])
{
int b;

b='A';
System.out.write(b);
System.out.write('\n');
}
}

The output is even though b is declared as an integer)
A

------------------------------------

If I change write() to println() and then try the output then it is 65.
This is not clear.

If I comment the last write() statement that is one containing '\n' then I see a blank screen as the output.This is also not clear

[edit]Disable smilies. CR[/edit]
[ June 06, 2008: Message edited by: Campbell Ritchie ]
 
Mohd Fuzail
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
print(int i)
Print an integer. The string produced by String.valueOf(int) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

write(int c)
Write a single character.

Hope it helps.

Next time please just do a quick reference to API before posting
[ June 06, 2008: Message edited by: Mohd Fuzail ]
 
Mohd Fuzail
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I comment the last write() statement that is one containing '\n' then I see a blank screen as the output.This is also not clear


From the source code of out.write


By default autoFlush is false, so if we write '\n' the data written is flushed from the stream or we can flush it explicitly

Hope that helps
[ June 06, 2008: Message edited by: Mohd Fuzail ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!