system.out.println(2+’ ’ +5) ;
Second output is not clear( data type part 9)
See in Java when you are printing something and you add a string after it say “hello”, everything after hello will be seen as a string only.
in case of 10+20+“hello”+10+20, the system computes the value of 10+20 which is 30 and currently everything encountered is an int. After it sees hello, everything after that becomes a string too. Now the 10 after hello is not an int anymore, it is seen as a string. Hence we only print it as 10, followed by 20 which is also seen as a string now.
In 2 + ’ ’ + 5, we are adding a space char (only space char, this is a character) after 2, hence 2 will be added to the ASCII value of space (’ ') and then 5 will be added to it.
If we use " " (this is a string with blank character), everything after the string is seen as a string.
Example:
system.out.println(2+’a’ +5); -> output is 104; ( this is evaluated as: 2 + 97(ascii value of ‘a’) + 5))
System.out.println(2 + “a” + 5); - > output is 2a5 (this is evaluated as: 2 + “a” + “5”)
but how the answer is 39