Sunday, 25 January 2015

Java System.out.write

I decided to try displaying a character on my screen using System.out.write. It accepts an integer parameter, which can even contain a letter, which seems strange to me. I could not get it to work at first:

andrew@UBUNTU:~/Java$ cat prog82.java
public class prog82     
  {
  public static void main(String args[])
    {
    int x = 'Y';
    System.out.write(x);
    }
  }
andrew@UBUNTU:~/Java$ javac prog82.java
andrew@UBUNTU:~/Java$ java prog82
andrew@UBUNTU:~/Java$


I found that I had to include a new line to force Java to display the output:

andrew@UBUNTU:~/Java$ cat prog83.java
public class prog83     
  {
  public static void main(String args[])
    {
    int x = 'Y';
    System.out.write(x);
    System.out.write('\n');
    }
  }
andrew@UBUNTU:~/Java$ javac prog83.java
andrew@UBUNTU:~/Java$ java prog83
Y
andrew@UBUNTU:~/Java$