Core java printing statement

415 views Asked by At

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)

10

There are 10 answers

0
Eran On BEST ANSWER

'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.

0
subash On

try something

sysoutln(""+'2'+'2'+"a");

then you can see 22a. when first string is occur in the expression then remaining will append with string. that mean + is consider as concatenation operation. before string occur in the expression, then that will evaluate as its own kick

2
Bhargav Kumar R On

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

0
Karthik Reddy V V On

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.

0
Anand Kulkarni On

Text entered in '' marked as char and "" marked as String.

2 has ASCII value is 50 (http://ascii.cl/index.htm?content=mobile)

  • operator always operate from left to right

Here 'a' will be considered as int and their ASCII value considered.

  • operator also act as concatenation depending upon associativity e.g 3+4+"a" = 7a and "a"+3+4 = a34

Hence '2'+'2+'a' = 100a

0
Revathi Bala On
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

0
AnkeyNigam On

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

0
aditya Goyal On

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

0
parag mundra On

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 :)

0
Java Rookie On

the compiler is explicitly converting you '2' to int value and concatenating it with the last String.Instead use this code;

System. out.println ("2"+"2"+"a");