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?
1 Antwort
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 ;)