4 ms·
Yes but that's strange. Java is such a staticaly typed language you would not expect it to do that if you are like me and don't know it very well. While Python
by sametmax 9y ago
Yes but that's strange. Java is such a staticaly typed language you would not expect it to do that if you are like me and don't know it very well.
While Python is very dynamic, but:
>>> print(1 + 2 + "=" + 1 + 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
- pmontra 9y agoRuby is like Python, Perl and JavaScript are like Java. Type coercion has little to do with being strongly or weakly typed, dynamically or statically typed, interpreted or compiled, which are orthogonal axis. It's more like syntactic sugar to save a str(), a .to_s or a printf pattern. Maybe the Java implementation just overloads the + operator and converts the types inside it.
- ubernostrum 9y agoStrong/weak is not a useful thing most of the time, but when people do try to define it, often they do so in terms of the presence and number of implicit conversions the language will perform for you in order to allow operations to succeed despite being provided operands of the wrong type. Also, overloading the "+" operator is basically what Java is doing, which is an odd inconsistency for a language which doesn't allow end-user code to do operator overloading.
- FascinatedBox 9y agoThis is possible exactly because Java is statically-typed. Java's type system works over the operands, and does implicit conversions before the expression gets a chance to run. It sees values and does implicit magic from left to right. Python, on the other hand, does not need to do a typing pass at parse-time. Instead, literals are stored as values, and those values are tagged with their source class. Python's way of doing it means that a + operation is called upon with an integer and a string. Java might be able to yield the same kind of result as Python if it had operator overloading. It could define Integer's + operator as only taking other Integer values, therein yielding a type error at parse-time. My own language uses ++ instead of + to distinguish between addition and concatenation, though I've considered some other token since the two are quite similar to each other.
- sametmax 9y agoActually Python does have a way to change addition semantics. It's just not defined for this operation.
- cakoose 9y ago> This is possible exactly because Java is statically-typed. Are you saying that Python can't make "a"+1 become "a1" because it lacks static typing? That's not true. If they wanted to, they could have added an overload for the string+integer case. > Java might be able to yield the same kind of result as Python if it had operator overloading. Not necessary. They would have just had to omit the built-in overload for string+integer (and perhaps also for string+object, given the newish auto-boxing feature).
- olliej 9y agoThis has literally nothing to do with static typing. It is 100% due to implicit promotion (demotion?) of addition operands to string if either operand is a string. All static typing does is mean that the type check, and so decision to insert the conversion call is done at compile time. Dynamic typing just means that the type check and decision to insert the conversion is done at the point the + is evaluated.
- pasquinelli 9y agodynamic vs. static typing is independent of strong vs. weak typing. strong typing is basically allowing fewer implicit converstations, weak more. static means type checking happens before runtime, dynamic means the opposite.
- guelo 9y agoThanks for that explanation. So Java is weakly typed? Or would you say less strongly typed? Is the strength determined by the amount of implicit type coercions that are available? Does static vs dynamic also imply that the strong-ness is checked at compile vs runtime? In other words, in python this bug can lurk undetected in your code list = (1, 2, 3) x = list + 1 while in Java it will throw the error at compile time int[] list = {1, 2, 3} Object x = list + 1
- sametmax 9y ago> In other words, in python this bug can lurk undetected in your code Well, if you use Python 3 and a modern editor, you will get a big red warning. mypy has been able to check types for a while and a lot of editors embed it.
- guelo 9y agoBut mypy is basically a static type checker for python. So my point still stands that static implies that strong-ness is checked at compile time, or IDE time in this case. Thinking about this more, if all that strong vs weak is referring to is implicit type coercions then it is a useless confusing term. Static vs dynamic refers to when the type checking occurs, implicit type coercions are just one of the things the type checker looks for.
- BerislavLopac 9y agoI've always seen the difference as this: - In statically-typed languages, variables are containers, which allow only objects of a certain type to be contained within. - In dynamically-typed languages, variables are more like labels which can be attached to different objects, which can be of different types. Each of these approaches have its merits, which is obvious when you look at all the lengths languages of one type go to to emulate the features of the other: interfaces and generics in statically-typed languages, or type hints and statical checkers in dynamic ones. But I have yet to see a language that would offer both types of variables... As a Python developer, I can easily imagine a PEP that would introduce some kind of "container variables" that would accept only properly typed objects and throw a syntax error if a violation occurs at compile time. But I'm not holding my breath, with a language which ironically doesn't even have an implementation for constants.
- c8g 9y agothat's also strange to me. you are seeing this error because python is strong typing language. but this is weird that java is also strong typing language[0] 0: https://en.m.wikipedia.org/wiki/Java_(programming_language) https://en.m.wikipedia.org/wiki/Java_(programming_language)
- Seol 9y agoStrong v weak typing isn't an absolute categorisation, it's a spectrum. There aren't many places Java will automatically coerce types, and the ones that do exist are narrow (numeric promotion, string concatenation, and I can't think of any others).