7 ms·
> Java, C# and JS use UTF-16-like encoding for in-memory string That’s incorrect for Java, possibly also for C# and JS. In any language where strings are opaq
by Someone 1y ago
> Java, C# and JS use UTF-16-like encoding for in-memory string
That’s incorrect for Java, possibly also for C# and JS.
In any language where strings are opaque enough types [1], the in-memory representation is an implementation detail. Java has been such a language since release 9 (https://openjdk.org/jeps/254 https://openjdk.org/jeps/254)
[1] The ‘enough’ is because some languages have fully opaque types, but specify efficiency of some operations and through it, effectively proscribe implementation details. Having a foreign function interface also often means implementation details cannot be changed because doing that would break backwards compatibility.
> JS use floating point for all numbers. The max accurate integer is 2⁵³−1
That is incorrect. Much larger integers can be represented exactly, for example 2¹⁰⁰.
What is true is that 2⁵³−1 is the largest integer n such that n-1, n, and n+1 can be represented exactly in an IEEE double. That, in turn, means n == n-1 and n == n+1 both will evaluate to false, as expected in ‘normal’ arithmetic.
- debugnik 1y ago> possibly also for C# and JS The representation for C# is very much fixed, as it allows, and very commonly uses, direct access into the string buffer as a ReadOnlySpan<char> or a raw char pointer, where char is the type of UTF-16 codepoints. JS could maybe get away with it.
- hinkley 1y agoWhen you have code that works a lot with strings the cost overhead of building an app on iso-latin-1 but encoding as utf-16 can be substantial. I think Java moved away from this back around 8, or possibly 9.
- scarface_74 1y agoI started to say something about C# strings and then I remembered the clusterfuck when it came to Windows development and strings and depending on which API you call, a string is represented by one of a dozen different ways. https://stackoverflow.com/questions/689211/interop-sending-string-from-c-sharp-to-c https://stackoverflow.com/questions/689211/interop-sending-s...
- deleted 1y ago[deleted]
- seangrogg 1y agoYeah, I think they didn't mean max "accurate" integer and rather meant max "safe" integer.
- mikojan 1y ago> > Java, C# and JS use UTF-16-like encoding for in-memory string > > That’s incorrect for Java, Maybe so, technically, but if you Base64 encode a string in a language that uses UTF-8 (or another UTF-16 with another endian) and decode it in Java, Java's UTF-16 representation will be the problem you will be dealing with.
- kelnos 1y agoThat's why when you are constructing a String with a byte array, you always, always, always use the constructor that also takes a character set.
- qouteall 1y agoThanks I will correct that