3

So I wanted to add a character to a string, and in some cases wanted to double that characters then add it to a string (i.e. add to it itself first). I tried this as shown below.

char s = 'X'; 
String string = s + s;

This threw up an error, but I'd already added a single character to a string so I tried:

String string = "" + s + s;

Which worked. Why does the inclusion of a string in the summation cause it to work? Is adding a string property which can only be used by characters when they're converted to strings due to the presence of a string?

Roman C
  • 49,761
  • 33
  • 66
  • 176
public static void
  • 1,153
  • 11
  • 20
  • 2
    possible duplicate of [How to concatenate characters in java?](http://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java) – Anderson Green Aug 13 '13 at 18:59
  • 1
    The error that it threw is probably the answer to your question ;) – bengoesboom Aug 13 '13 at 19:01
  • A character can only be a length of 1. Java doesn't "know" you are going to assign this expression to a String. – Peter Lawrey Aug 13 '13 at 19:05
  • Similar Question [How to concatenate characters in java?](https://stackoverflow.com/questions/328249/how-to-concatenate-characters-in-java) asks/answers HOW to construct a String from >1 char. This Question asks/answers WHY attempting to do so with '+' operator is unsuccessful; worth asking (duplicate flag therefore retracted, and +1 added here :) – cellepo Nov 21 '18 at 02:39
  • Please clarify the final sentence of your Question - what does it mean? @SahilMahajanMj do you know? – cellepo Nov 23 '18 at 19:16
  • @K.Nicholas do you know ^? – cellepo Nov 23 '18 at 19:16

7 Answers7

7

It's because String + Char = String, similar to how an int + double = double.

Char + Char is int despite what the other answers tell you.

String s = 1; // compilation error due to mismatched types.

Your working code is (String+Char)+Char. If you had done this: String+(Char+Char) you would get a number in your string. Example:

System.out.println("" + ('x' + 'x')); // prints 240
System.out.println(("" + 'x') + 'x'); // prints xx - this is the same as leaving out the ( ).
Xabster
  • 3,710
  • 15
  • 21
  • 1
    Well, you meant `char` and not `Char`, but nice line about ints regardless :) – keyser Aug 13 '13 at 19:08
  • I actually wrote char/Character initially, but then I didn't want to keep it up so I deleted and went compromise. :) You're right though. Char isn't anything in Java. – Xabster Aug 13 '13 at 19:19
  • So at this point, why keep "Char"? Why not use one of {`char`, `Character`, 'char/Character'}? I'm not sure why keeping "Char" is a better compromise? I'd edit it myself, but want to make sure I'm not missing something... – cellepo Nov 21 '18 at 01:24
4

char + char returns an int so the compiler complains that String string = (int), which is indeed wrong.

To concatenate the chars, you can use the empty String ("") at the beginning so the + operator will be for String concatenation or use a StringBuilder that can append chars as well.

char s = 'X';
String string = new StringBuilder().append(s).append(s).toString();

Note: the char variable is s, not X.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 1
    @Pshemo `char`+`char`=`int`. Try this: `char a = 'a', b = 'b', c; c = a + b;` – Luiggi Mendoza Aug 13 '13 at 19:15
  • +1 and to add some info: result of `char1` + `char2` is `int` that is sum of positions of `char1` and `char2` in [UnicodeTable](http://unicode-table.com/en/). So for `'X'+'X'` result will be `88 + 88` = `176` which if OP would like to cast to char would be `°`. – Pshemo Aug 13 '13 at 19:23
3

In Java, char is a primitive integral numeric type. As such, the operator + is defined to mean addition of two chars, with an int result. The operator means concatenation only on strings. So one way to do it is

"" + char1 + char2

This will coerce the right-hand operand to a string, which is what you want to achieve.

A side point: char is the only unsigned primitive numeric type in Java and in one project of mine I specifically use it as such.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Thank you for the "_unsigned_ primitive numeric type" (emphasis "unsigned") side note - I didn't realize that before. – cellepo Nov 21 '18 at 01:07
0

When you do char s = 'X'; String string = X + X;

Why not do this instead? char s = 'X'; String string = s + s;

kelorek
  • 6,042
  • 6
  • 29
  • 32
0

Adding in the "" changes the return type to a string. Leaving it out means the return type is a char which doesn't match.

Andrew_CS
  • 2,542
  • 1
  • 18
  • 38
0

String string = "" + X + X;

is an example of concatenation. By pre-pending the empty string you specify that the result should be a string. The first line of code tells the compiler that you are adding 2 chars (or ints) which should result in an int, and not a string.

Clocks
  • 411
  • 4
  • 8
0
 String string = X + X;

Here X i'ts threated as a variable

You should use something as

String string ="x x";

Or

String x = "something";
String y = "else";

then String string= x+y; should work just fine, this is because you are concatening with the "+" sign, you could also use

string = string.concat(x+y); 

or

string = string.concat("something"+"else");
Abstract
  • 664
  • 1
  • 5
  • 15