Java references always point to an object. The object has a header that amongst other things identifies the concrete type (so casts can fail with ClassCastException
). For arrays, the start of the object also includes the length, the data then follows immediately after in memory (technically an implementation is free to do what it pleases, but it would be daft to do anything else). So, you can;t have a reference that points somewhere into an array.
In C pointers point anywhere and to anything, and you can point to the middle of an array. But you can't safely cast or find out how long the array is. In D the pointer contains an offset into the memory block and length (or equivalently a pointer to the end, I can't remember what the implementation actually does). This allows D to slice arrays. In C++ you would have two iterators pointing to the start and end, but C++ is a bit odd like that.
So getting back to Java, no you can't. As mentioned, NIO ByteBuffer
allows you to wrap an array and then slice it, but gives an awkward interface. You can of course copy, which is probably very much faster than you would think. You could introduce your own String
-like abstraction that allows you to slice an array (the current Sun implementation of String
has a char[]
reference plus a start offset and length, higher performance implementation just have the char[]
). byte[]
is low level, but any class-based abstraction you put on that is going to make an awful mess of the syntax, until JDK7 (perhaps).