System.out.write() in Java-not printing the least significant of an integer value

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



System.out.write() in Java-not printing the least significant of an integer value



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?


'A'





What did you expect to be printed?
– Eran
Aug 6 at 10:21





Do you want to achieve, that 1 is printed to console?
– r3dst0rm
Aug 6 at 10:25





Yes,I wanted 1 to be printed.
– Vaibhav Ranjith
Aug 6 at 14:43




3 Answers
3



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



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:


'A'


int


'A'


65


System.out::print


System.out::write


System.out.println(b);


65


System.out::print(int x)


x



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:


System.out.write(b);


A


System.out::write(int b)


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.





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 6 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 character will be print because it belows under number DEC 1 in the ASCII table.
– Nikolas
Aug 6 at 14:57


'1'


1


'1'


'1'


write(int b)


'1'


49


1


1


1





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 6 at 15:08





Yes, because the IDE cannot display these characters correctly and replaces them with this character.
– Nikolas
Aug 6 at 19:14



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".






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard