As my title suggests, it\'s a theoretical question. I\'d like to know that if java defines string as null terminated.
I'd like to know that if java defines string as null terminated.
No. A String is defined to be a fixed length sequence of char values. All possible char values (from 0 to 65535) may be used in a String. There is no "distinguished" value that means that the string ends1.
So how they track string ending? Using length?
Yes. A String object has a private length field (in all implementations I've examined ...).
If you want to understand more about how Java strings are implemented, the source code for various versions is available online. Google for "java.lang.String source".
1 - As noted, neither the JLS or the javadocs for String specifically that a String implementation cannot use NUL termination. However, the fact that all characters including NUL are significant in a String means that NUL termination is not practical.