1

This is my code:

class writedemo {
    public static void main(String args[]){
      int b;
      b=1;
      System.out.write(b);
      System.out.write('\n');    
    }
}

The output I got was the 'apl functional quad question' character(U+2370).

But this code worked:

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

It prints the character 'A'. Can someone please help me? Am I missing anything?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183

3 Answers3

2

System.out.write() store and prints the ASCII value. You can use System.out.print() to show your integer value. Difference of both is given below:

System.out.write(65);  //will print ASCII value of 65 which is A;
System.out.print(65); // will print just 65
Saboor
  • 352
  • 1
  • 10
1

If you pass a character value 'A' to int, the numeric ASCII value is saved. According to the ASCII table the glyph 'A' is converted to 65 as an decimal value. Here is the difference between the methods System.out::print and System.out::write which might be confusing:

  • System.out.println(b); prints 65 because in System.out::print(int x) the x is understood as an integer:

    Prints an integer and then terminate the line.

  • System.out.write(b); prints A because in System.out::write(int b) the b is understood as a byte:

    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.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • Thanks, but when I pass the integer value '1' to write(int b). Should'nt it actually print the ASCII equivalent of it's least significant byte? – Vaibhav Ranjith Aug 06 '18 at 14:49
  • @VaibhavRanjith: Do you pass `'1'` or `1`? - There is a big difference! If `'1'` If you pass `'1'` as a character to `write(int b)`, the character `'1'` is converted to `49` as ASCII decimal value and the method converts it back and print out the `1`. If you pass the `1` as an integer itself to the very same method, the [Start of Heading](https://en.wikipedia.org/wiki/C0_and_C1_control_codes#SOH) character will be print because it belows under number DEC `1` in the ASCII table. – Nikolas Charalambidis Aug 06 '18 at 14:57
  • Yeah that's what I would expect too. But the code prints the U+2370 character called the app functional quad question character. – Vaibhav Ranjith Aug 06 '18 at 15:08
  • Yes, because the IDE cannot display these characters correctly and replaces them with this character. – Nikolas Charalambidis Aug 06 '18 at 19:14
1

As mentioned by Maantje in his answer.

write(int) interprets the argument as a single character to be printed, while print(int) converts the integer into a character string. write(49) prints a "1", while print(49) prints "49".

Tarek Badr
  • 847
  • 9
  • 12