3 ms·
In some cases, Java is more weakly typed than Python or Ruby. Java: System.out.println("abc" + 5); -> "abc5" Python: print "abc" + 5 -> TypeError: cannot co
by meowface 13y ago
In some cases, Java is more weakly typed than Python or Ruby.
Java:
System.out.println("abc" + 5); -> "abc5"
Python:
print "abc" + 5 -> TypeError: cannot concatenate 'str' and 'int' objects
Ruby:
puts "abc" + 5 -> TypeError: can't convert Fixnum into String
- masklinn 13y agoAnd here we can see the issues and clusterfucks of the completely undefined and arbitrary "concept" of "weak" (conv. strong) typing: java's behavior here is well-defined, simple and statically typed, there's no more type loss than when doing `5 + 1.0` in Ruby or Python. Why do you find implicitly converting an int (/Fixnum) to a float (/Float) more palatable, when they have completely different internal representations, domains of definition, precisions, operational semantics and potential pitfalls? And what about Python's `"foo" * 3`? Hell, what about this: > class Fixnum > def to_str > self.to_s > end > end => nil > "5" + 3 => "53" so "strongly typed" it has a protocol for implicit string conversion. But wait, there's more without even bothering with overrides: > a = [1, 2, 3] => [1, 2, 3] > a[1.4] => 2 floating-point index to an array? no problem.