File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Can anyone please explain the code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can anyone please explain the code?" Watch "Can anyone please explain the code?" New topic

Can anyone please explain the code?

Shankar Salunkhe

Joined: Jul 17, 2012
Posts: 10

Can anyone please explain the below code.
CODE : -

public class SampleClaass{

public static void main(String []args){
int a;

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

Joined: Oct 02, 2003
Posts: 11923

Try this:

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Carina Caoor

Joined: Jan 21, 2013
Posts: 7
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

    Joined: Oct 13, 2005
    Posts: 45345
    You might get a hint from reading what the char datatype actually is. You will find it here in the Java Language Specification.
    I agree. Here's the link:
    subject: Can anyone please explain the code?
    It's not a secret anymore!