• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anyone please explain the code?

 
Shankar Salunkhe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone please explain the below code.
CODE : -

public class SampleClaass{

public static void main(String []args){
int a;
a='A';
System.out.write(a);
System.out.write('\n');
}
}

Why there is \n require for getting the output as : A.

If i removed the second System.out.write('\n');. The code get compile but unable to print the output.

Thanks in advance.
Shankar Salunkhe
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:

 
Carina Caoor
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the docs 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


  • By the way just curious to know why the character is assigned to int datatype?
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49472
    64
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You might get a hint from reading what the char datatype actually is. You will find it here in the Java Language Specification.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic