Go Data Structures (2009)(research.swtch.com) |
Go Data Structures (2009)(research.swtch.com) |
That is no longer the case in Java. String.substring() now makes a copy. I think it doesn't matter much which of the two approaches a language takes as long as everybody knows it. This needs to be in the language spec and can't be an implementation issue.
package main
import "fmt"
import "reflect"
func main() {
i := 3
z := reflect.ValueOf(i)
fmt.Printf("%s\n", z.Kind())
}
// $./test
// int
// $
It's my understanding that this intentional and won't change, only explicit declarations of int64 are 64-bit.There is also a set of predeclared numeric types with implementation-specific sizes:
uint either 32 or 64 bits
int same size as uint
uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value
As for shorthand, behold!
type any interface{}
[1]: http://golang.org/pkg/sort/#Interface(Unless I'm mistaken here, which might very well be the case.)
g% cg -c -f 'g/go/src/pkg.*\.go' '\bnew\(' | total 2
1485
g% cg -c -f 'g/go/src/pkg.*\.go' '\&[A-Za-z0-9_.]+\{' | total 2
3051
g% cg -c -f 'g/go/src/pkg.*\.go' . | total 2
430482
g%
So 430,482 non-blank lines of code, 1485 lines with new, 3051 lines that look like a struct pointer literal.Anyway, that's the point of go's type inference- if the object implements the necessary parts of the interface, it counts as that kind of object.
If I'm following, a Java string used to be a []char and offset/count ints, and this change let them drop those ints. You saved RAM if you had a lot of little strings, but paid for extra copying if you took lots of substrings.
Go slices/strings don't have a pointer to the "original" backing array, just a pointer to the first byte in this (sub)string. It doesn't need extra fields to do substrings by reference.
I think part of the technical reason for the different string headers is that the Java designers didn't want their GC to have to handle "internal pointers" into strings/objects (maybe for performance reasons?), whereas the Go designers decided to support 'em (maybe to support more C-like code in Go?).
Go's GC's support for internal pointers means it can use a pointer-and-length representation for substring references. Java's lack of support for them means its string representation needs a pointer to the start of the char array and a separate offset and count in order to do the same substring-reference trick. (And, I'm saying, that helps explain why Java and Go now do substrings differently.)
There are other places where Go's ability to use internal pointers is exposed more directly to the programmer: for example, Go lets you take the address of an array element or struct field and pass around the resulting pointer.
Only if the String class is implemented in pure Java, which it currently is. But it doesn't have to be that way. Oracle could go around the Java language features and implement the String class in native code just as Go does with several builtin types. You may be right that it would be more difficult to do than in Go because of garbage collector specifics.
But I guess the real issue is a philosophical one. Is it a good idea to let the standard library use features that are not available to users of the language?
Given that GC design, Go's two-word substring references (pointer into middle of string + count) wouldn't work; even if String were a builtin, with the no-internal-pointers GC design it would need to be at least three words (pointer to start of string, offset, count).
tl;dr of my larger point is--I think Java needed a few extra bytes/String to support substrings by reference because of how its GC works differently from Go's, and I think that explains why Java decided to remove its substring-by-reference trick while Go didn't. (And I'm not trying to say either way is worse, just trying to really grok why they're different.)