2

In a programming language called Java, I have the following line of code:

char u = 'U';
System.out.print(u + 'X');

This statement results in an output like this:

173

Instead of

UX

Am I missing something? Why isn't it outputing 'UX'? Thank you.

assylias
  • 321,522
  • 82
  • 660
  • 783

5 Answers5

3

In this language known as Java, the result of adding two chars, shorts, or bytes is an int.

Thus, the integer value of U (85) is added to the integer value of X (88) and you get an integer value of 173 (85+88).

To Fix:

You'll probably want to make u a string. A string plus a char will be a string, as will a string plus a string.

String u = "U"; // u is a string
System.out.print(u + 'X'); // string plus a char is a string
Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
3

Because you are performing an addition of chars. In Java, when you add chars, they are first converted to integers. In your case, the ASCII code of U is 85 and the code for X is 88.

And you print the sum, which is 173.

If you want to concatenate the chars, you can do, for example:

System.out.print("" + u + 'X');

Now the + is not a char addition any more, it becomes a String concatenation (because the first parameter "" is a String).

You could also create the String from its characters:

char[] characters = {u, 'X'};
System.out.print(new String(characters));
assylias
  • 321,522
  • 82
  • 660
  • 783
0
String u = "U";
System.out.print(u + "X");

instead of char type use String class or StringBuilder class

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

Another way to convert one of the characters to a string:

char u = 'U';
System.out.print(Character.toString(u) + 'X');

This way could be useful when your variable is of type char for a good reason and you can't easily redeclare it as a String.

ajb
  • 31,309
  • 3
  • 58
  • 84
-1

That "language called Java" bit amused me.

A quick search for "java concatenate" (which I recommend you do now and every time you have a question) revealed that most good Java programmers hate using + for concatenation. Even if it isn't used numerically when you want string concatenation, like in your code, it is also slow.

It seems that a much better way is to create a StringBuffer object and then call its append method for each string you want to be concatenated to it. See here: http://www.ibm.com/developerworks/websphere/library/bestpractices/string_concatenation.html

Wutaz
  • 362
  • 3
  • 19
  • (i) `+`, when used for String concatenation in a single statement will be automatically converted to StringBuilder#append at compile time. It is only when you concatenate several times (typically in a loop) that your comment applies. (ii) the article your link to is obsolete - you should almost never use StringBuffer and you should use StringBuilder instead. – assylias Sep 16 '13 at 21:05
  • Since that article was written in 2001, I'd question whether the lab tests quoted in the article (comparing the efficiency of the two approaches) are still valid. A clever compiler *could* ensure that something like `System.out.println(aStudent + " received a grade of " + aGrade);` is no less efficient than the `StringBuilder` approach. I'd agree that using `StringBuilder` is better than using `+=` in a loop, but it seems heavy for a simple expression like the above. – ajb Sep 16 '13 at 21:25
  • Oops. Normally searching for the answer is a good idea, but I guess it doesn't compensate for not knowing the language. – Wutaz Sep 16 '13 at 21:27
  • 1
    Plus, the recommended sample code in this "best practices" article would output "James Bondreceived a grade ofA". There are some needed spaces missing. Is it a good idea to trust the "best practice" recommendation of an author who can't even get that right???????????? I guess he didn't think "testing one's code before publishing it in an article" was a Best Practice. :) – ajb Sep 16 '13 at 21:28