Why does java compiler gives
100a
as output when I ever tried to print System.out.println('2'+'2'+"a")
and
a22
for System.out.println("a"+'2'+'2')
. Please explain in detail . thank you)
You have to understand two things here.
+ operator when used with atleast one string the result will be string. If both are numbers the result will be number(Polymorphic).
The other thing is, double quote denotes string where as single quote denotes a character(which internally means ASCII int value). Hope this helps
Java uses Unicode to represent characters.
In System.out.println('2'+'2'+"a"), the first operand is char '2' and its Unicode(ASCII) value is 50. So it adds up to 100 and then concatenates with the String literal "a". So the output is 100a
In System.out.println("a"+'2'+'2'), since the first operand is String literal, it is considered to be String concatenation and hence the output is a22.
Text entered in '' marked as char and "" marked as String.
2 has ASCII value is 50 (http://ascii.cl/index.htm?content=mobile)
Here 'a' will be considered as int and their ASCII value considered.
Hence '2'+'2+'a' = 100a
System.out.println('2'+'2'+"a");
Here 2 represents the char value because it is inside the single quotes...so you are passing the value for '2' is 50 so it just add both the values (50+50=100)so it appends 100 at the place of('2'+'2')..and then here the a is string because you created by using double quotes so it just appends that a after the value of('2'+'2')...and also note that inside double quotes if you are giving something its is printed as it is...so the output is 100a
System.out.println("a"+'2'+'2');
Here it performs string concatenation because the first operand is string...so you get that a22..note that after any string operand if you give any integer or char value it just take that as string only..
In the first statement your first operand is char so both char values are added and then it performs string concatenation but in second statement your first operand itself string so that's the diff between these two.
I hope you understand now
If you are doing this System.out.println('2'+'2'+"a")
this will simply append the ascii value of 2, which is 50, as it is in single quotes, treating it as a char, twice which becomes 100 and finally appending "a" in the end so becoming 100a. The thing to consider here is that the parameters starts with a char value.
If you are doing this System.out.println("a"+'2'+'2')
it will just append the '2' twice to "a" as it starts with String and '+' will be working here as a concatenation operator, so making it finally a22
Note:
single quotes: char,
double quotes: string
if you are using single quotes first with maths operator's without using string before single quotes. Then, compiler works with their ascii values.
For example:
'2'+'2' = 100
"2"+'2' +"a"= 22a
'a'+'a' = 194
"a" +"a" = aa
'a' +'2' = 147
"a" + '2' = a2
https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
Here you will get the ASCII values. In '2'+'2'+a, as it read from left to right the ASCII values of character 2 is 50, due to single quotes, so the result become 100a. And in second scenario System.out.println("a"+'2'+'2'), a is a string(double quotes), so it is containing the string and result become a22.
And the result for System.out.println('a'+'2'+'2'); is 197 :)
'2' is a char, so '2' + '2' adds the int value of that character to itself (50+50) and then appends "a" to it, giving you 100a.
"a" + '2' + '2' performs String concatenation, since the first operand is a String. Therefore you get a22.
Note that the expressions are evaluated from left to right, so the types of the first two operands determine whether + will perform an int addition or a String concatenation.