C
C#7d ago
Jexs

✅ Is my understanding correct?

In this code:
double averageAge;

averageAge = (43 + 22 + 62)/3 output: 42
-vs-
averageAge = 43 + 22.0 + 62)/3 output: 42.333333333333336
double averageAge;

averageAge = (43 + 22 + 62)/3 output: 42
-vs-
averageAge = 43 + 22.0 + 62)/3 output: 42.333333333333336
Notice that theses two equations (both are the same) one has 3 whole numbers and the second has a decimal/Double with whole numbers (just adding a .0 to 22 i guess makes it a double/Decimal ) . The compiler assumes if you use all integers your output will be an integer but if you use a double/Decimal even one the compiler will assume you want a double/Decimal as an output. right? Thanks! 🙂
6 Replies
many things
many things7d ago
yes, but you can also use literals so for example 43 + 22F + 62 would change expression type to float
Jexs
JexsOP7d ago
okay thank you 🙂
Anton
Anton7d ago
Yes, but no. It is important to understand the mechanism. These aren't equations, these are expressions. Expressions are evaluated at runtime. It means that you substitute concrete values instead of variable names, go through the arithmetic with concrete numbers, go through function calls until you reach a point where everything simplifies down to a single value, which is the result of expression evaluation. Expressions are made up (in your case) of operators acting on the results of other expressions (operands), be it concrete number literals in your code or more complex expressions (as described above). For example, the operator + works on 2 operands (it's a binary operator), which can be the result of any sort of expression. (1 * 5) + (2 * 3) the + operator is acting on the results of the expressions (1 * 5) and (2 * 3), so they will be evaluated first, then the plus. (Generally it's like this, but logical operators have the ability to skip the evaluation of the expression of one of the operands, not important.) / is likewise an operator. But there are multiple kinds of either operator, which work with the different types of operands. Both operands being ints, longs, floats, doubles - there are different versions of any operator for these. Which one will be used is dictated by the type of the expression of the operand. Aka if you do 1.0 / 2.0, then the double version of / will be used. 1.0 / 2 however still does the double version of the division operator. How can it do that if one of the operands is an int and not a double? There's another idea in play, called an implicit conversion or maybe a number promotion, more specifically. The compiler converts that expression to something like 1.0 / ( convert_to_double_from_int(1) ). It would always select the larger number type of the 2 operands to convert to. Another example: 1 + 2 + 3.0 This really will do ((1 + 2) + 3.0) because + is a binary operator, you apply the second one to the result of the first one. so you evaluate (1+2) the nested expression to evaluate the other + you're left with 3 + 3.0 now the integer will be promoted to the larger of the 2 number types. so that will really do convert_to_double(3) + 3.0 which will amount to 3.0 + 3.0 = 6.0 so the original expression, if rewritten more explicitly, would look like this: convert_to_double_from_int(1 + 2) + 3.0 and will be an expression of type double (evaluating to double in the end). One more important idea to understand is that the conversion will be inserted at compile time, by analyzing the types of all expressions by the compiler, and not at runtime by looking at the actual types of the numbers (like in e.g. Python). This is what's referred to as static typing, where any expression has its type known at compile time. (In case of OOP the actual dynamic type might be different than the statically known type, but that's a different story.)
ero
ero7d ago
holy chat gpt
Anton
Anton7d ago
no I literally typed this out myself took me a while though
ero
ero7d ago
wowza!

Did you find this page helpful?