Why cant print 2 char with sysem.out.print command [duplicate]

156
July 27, 2019, at 3:30 PM

This question already has an answer here:

  • Why can I add characters to strings but not characters to characters? 7 answers
  • Calculating with the char variable in java 2 answers

I cant print two char in one system.out.print code as it is shown in below. I would like to know how java works in that case since it is summing up ASCII conversions of these chars.

System.out.println('a'+'b');
Answer 1

This is Java, not JavaScript.

In Java, the single quote is reserved for char data. You must use the double quote for a String value.

As stated in the @Sean Bright comment, char + char is math, not string concatenation.

There are many ways to output two char values. Here is an example of one such way:

final String output;
output = String.format("%c %c\n", 'a', 'b');
System.out.println(output);
Answer 2

The output of the last one is 195, because you are doing an addition of char values in the ASCII table (97+98) . you first sum all the values as explained before, then convert it to a String adding "" to the sum of the ASCII values of the chars. However, the println method expect a Strin Y

READ ALSO
How to print only time in java? [duplicate]

How to print only time in java? [duplicate]

This question already has an answer here:

213
Outlook wont preview ics file created by ical4j

Outlook wont preview ics file created by ical4j

When sending ics attachments created by ical4j via sendgrid to an outlook user they aren't able to "preview" the inviteIt recognizes that the attachment exists but claim "This file cannot be previewed because there is no previewer installed for it"

151
Return Array from Java to Python

Return Array from Java to Python

Greetings i'm trying to return Array to Python from java(jar file) but only return array type string:

212
Alert message validation - Test Automation (Selenium)

Alert message validation - Test Automation (Selenium)

Good Morning! I'm catching a lot in a settingCould you help me?

110