14

As my title suggests, it's a theoretical question. I'd like to know that if java defines string as null terminated.

4
  • @Reimeus, so how they track string ending? Using length? Commented Aug 24, 2013 at 4:20
  • Didn't see that question although searched google before. May be a duplicate! Commented Aug 24, 2013 at 4:28
  • Dewsworld, stating it's a theoretical question is almost a dead-cert reason for closing. SO is supposed to be for actual problems that you face. Commented Aug 24, 2013 at 4:28
  • 2
    @MatteoD - That question is actually about something different. It has a misleading title. Commented Aug 24, 2013 at 4:58

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

Comments

10

Java strings are not terminated with a null characters as in C or C++. Although java strings uses internally the char array but there is no terminating null in that. String class provides a method called length to know the number of characters in the string.

Here is the simple code and its debugger contents:

public static void main(String[] args) {
    String s = "Juned";
    System.out.println(s);
}

Debugger screenshot:

enter image description here

7 Comments

Do you have any reasoning to support your answer?
@Juned Ahsan, Can you please explain a little :)
The java specification does not mention it but if you run your code in debug mode, you can alaways see the contents of String internal array, and there will be no terminating null character.
Java is the JLS, which doesn't mandate null-termination but doesn't disallow it either. The fact that one implementation of Java doesn't do it is less relevant than you think. I won't vote you down but you may want to make the distinction between the Java specification and implementations.
Can you explain the significance of the screenshot?
|
0

Does it matter?

If you convert a Java string into some kind of serialized format (onto disk, the network, etc.), then all that matters is the serialization format, not the JVM's internal format.

If you're reading the string's data in C code via JNI, then you never read the data directly, you always use JNI functions like GetStringChars() or GetStringUTFChars(). GetStringChars() is not documented as returning null-terminated data, so you shouldn't assume that it's null-terminated—you must use GetStringLength() to determine its length. Likewise with GetStringUTFChars(), you must use GetStringUTF8Length() to determine its length in modified UTF-8 format.

4 Comments

"Does it matter?" Umm... it's nice to know how computers work.
It does matter if you are worried about what happens if you put a NUL character into a String. However, if you are doing that, it is arguable that you are using String incorrectly ... because a NUL character should not appear in real text. (But I accept that there are use-cases where you would want to allow NUL in a String anyway.)
It does matter: Today I was assuming that they were NUL terminated, and escaped a \0 in the middle of a string just to test a reverse() function, then reversed; and to my surprise, it reversed it even after the \0, so I came here to see why :)
Adam, although you've made your point pretty clear, you you didn't answer the question. Upvoted it because you called our attention to an interesting issue in a provocative way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.