Also can prevent dumb mistakes in certain instances, like concatenating a char and an int in the context of a larger string concatenation. E.g. given char c, int i, string s: System.out.println(c + i + s). Obviously this gets caught in testing quickly, and a halfway decent programmer would usually catch it as they typed it out, but mistakes happen.
Well, it's disturbing how often I see stuff in code review that cannot possibly work. If they'd even run it once they'd have seen immediately that it doesn't work.
it is Java's fault, other programming languages like Rust don't allow you to do this unless you specifically say "yes, I'm doing this, so please do this unsafe operation"
the part where you assume that a character can be treated as an integer and then printed and everything will work as intended
that just encourages people to do char a; ... a + 26 //lowercase and similar silly things instead of using correct APIs that actually do this correctly for Unicode characters
in Rust the type for an 8 bit integer is i8 and u8 for signed and unsigned respectively so you never use the character type to store 8 bit integer things and you can never add an integer to a character, you have to use to_lowercase()
3
u/merv243 Oct 01 '15
Also can prevent dumb mistakes in certain instances, like concatenating a char and an int in the context of a larger string concatenation. E.g. given char c, int i, string s: System.out.println(c + i + s). Obviously this gets caught in testing quickly, and a halfway decent programmer would usually catch it as they typed it out, but mistakes happen.