Help me with another ":" example also other question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help me with another ":" example also other question

I've seen that someone defined a string the made it 123 made a ":" and made another definition of that string. Also I've seen someone add for example 8+""+3 what does it stand for?

21st Feb 2017, 11:28 AM
Sebastian Rumpel
Sebastian Rumpel - avatar
1 Answer
0
When you add two variables with the same type, the result will be their type (usually). So, if you evaluate the expression 5+2 it will be an integer with the value of 7. But, if you add numbers with different types, there are two cases. The first, is that they cannot be added. Like an object and a number cannot produce a summary. The other case is, when there is a dominant type. The String type is highly dominant, and if you add a number and a string, then the result will be a string. Like: int sum = 5 + 2; string sum2 = "Sum is: " + sum; Here I added the "Sum is: " string and the number variable 'sum' together. Their summary produced a string, which I stored in the 'sum2' variable, with the value of "Sum is: 7". In your example, 8+""+3 will produce a string, because it is evaluated the following way: 8 is added to "" ---> "8" "8" is added to 3 ---> "83" So the result of the expression 8+""+3 will be "83". Not all programming language supports the addition of different types, but most of the do support adding any kind of number to produce a number with the larger scale / dominant type. Like: double + integer = double short + integer = integer And also, generally string concatenation with numbers is also supported: number + string = string Hope I helped ;)
25th Apr 2017, 5:38 PM
Magyar Dávid
Magyar Dávid - avatar