Adding character/s to a string has two different variations.

If you add two characters to a string you get an int as an output but when you add one character it works. Please explain. Here is an example with output for reference.
String str = "eeksforGeeks";

// Inserting at the beginning
String str2 ='[' +'['+ str;

// Print and display the above string
System.out.println(str2);
str2='['+str;
System.out.println(str2);
String str = "eeksforGeeks";

// Inserting at the beginning
String str2 ='[' +'['+ str;

// Print and display the above string
System.out.println(str2);
str2='['+str;
System.out.println(str2);
Output-: 182eeksforGeeks [eeksforGeeks
8 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @theash2473! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan3w ago
That's an unfortunate result of the fact that operator + may mean addition or concatenation And also of the fact that Java sees the char type as a number with weird literals If you apply operator + with any of its two operands being a non-wrapped object, then it is concatenation. It will produce a String which is the result of converting operands to String as needed, and then producing the String that is the first operand's String immediately followed by the second's. If you apply operator + on two number operands, then it is addition. And chars are seen as numbers. So it adds '[' and '[', which is 182. Then it concatenates 182 with "eeksforGeeks"
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
theash2473
theash2473OP3w ago
Forgot to mention what I found. If first two operands has a string then it will become a string no matter how many oeprands. Otherwise it will be converted to an int.
theash2473
theash2473OP3w ago
Stack Overflow
In Java, is the result of the addition of two chars an int or a char?
When adding 'a' + 'b' it produces 195. Is the output datatype char or int?
theash2473
theash2473OP3w ago
Sample code to understand all cases.
String str = "eeksforGeeks";
String str2 = '[' + '[' + str;// first two operands is char so it becomes an int
System.out.println(str2);
str2 = '[' + str;// string gets concatenated with char and returns a string
System.out.println(str2);
str2 = 'a' + "" + 'b';// Trick to convert concatenated chars to a string where "" has to be put among the first two operands
System.out.println(str2);
str2 = 'a' + 'b' + "";// chars get converted to int instead because "" is not among the first two operands
System.out.println(str2);
str2 = str + 's' + 's';// concatenated successfully to string str
System.out.println(str2);
str2 = "" + 's' + 'a' + 'd';// Once it is converted to a string through "" we can add multiple chars to it in the same statement
System.out.println(str2);
String str = "eeksforGeeks";
String str2 = '[' + '[' + str;// first two operands is char so it becomes an int
System.out.println(str2);
str2 = '[' + str;// string gets concatenated with char and returns a string
System.out.println(str2);
str2 = 'a' + "" + 'b';// Trick to convert concatenated chars to a string where "" has to be put among the first two operands
System.out.println(str2);
str2 = 'a' + 'b' + "";// chars get converted to int instead because "" is not among the first two operands
System.out.println(str2);
str2 = str + 's' + 's';// concatenated successfully to string str
System.out.println(str2);
str2 = "" + 's' + 'a' + 'd';// Once it is converted to a string through "" we can add multiple chars to it in the same statement
System.out.println(str2);
Kyo-chan
Kyo-chan3w ago
Right. It's what I said above, and operators are evaluated left to right
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server