Java strings are not null terminated. They end with the length in length. You can make a \0 the last character of a Java string, but that doesn't automatically terminate the string. The length of 12<\0>45 would still be 5 and not 2 as in C.

Answer from Thorsten Dittmar on Stack Overflow
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 730461 โ€บ java โ€บ null-null-character
null? null character? (Beginning Java forum at Coderanch)
May 14, 2020 - If some reference is set to NULL this means: "This refernce does not 'point' to anything.". In addition to that: "text" often is refered to "readable" - which limits the range from "full" ASCII 0x00 - 0x7F to only 0x20 - 0x7E as the first 32 codepoints are the basic control characters which don't have a printable representation and 0x7F is the DEL (delete) character - which actually has its origins back on punch cards where when you made a mistake instead of fixing the wrong holes one would had punched out ALL holes which had the special meaning "This character was deleted.". Why DEL made it i
Discussions

How to represent empty char in Java Character class - Stack Overflow
The correct answer, already given by user3001, is to use Java's boxed Character reference type and give it 'null' value when 'no character' is needed. 2014-08-02T03:24:15.473Z+00:00 ... @AVD Yes, and rahulsri asks how to represent a missing character. So a null Character reference fits. 2014-08-03T00:08:26.81Z+00:00 ... That means that there is no char value for which String... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Null character '\0' & null terminated strings
should the strings be terminated by NUL in that character set, or by a character whose value is zero? The character '\0' is guaranteed to be a byte with all bits zero, and to have a numeric value equal to zero. A string in C always ends with this character. Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. No, in standard UTF-8 the code point with value zero is encoded in a single zero byte. You may have been reading something about "modified UTF-8", which appears to be a rather Java-centric external encoding for strings. It deliberately uses an "overlong" encoding of Java '\u0000' so that the resulting byte sequence does not contain a zero byte. One reason for this is because the length of strings in Java is not defined by use of a terminating character โ€” a Java string can contain arbitrary '\u0000' characters โ€” and you might need some way to round-trip such strings between Java and a language like C that does use a zero byte as a terminator. More on reddit.com
๐ŸŒ r/C_Programming
14
17
December 25, 2022
Is a null character really the most efficient way to mark the end of a string in memory?
What do you mean by "one byte represent multiple separations"? I'm confused what your string would actually look like in memory. Modern languages for the most part do not use the null character to mark the end of a string. It is generally recommended to explicitly keep track of string length, avoiding unexpected string truncations or accidental buffer overflows. It comes from a time where memory was very limited, so the single byte to mark the end of a string was more efficient than potentially using multiple bytes to store the length. Because C is so ubiquitous we can't actually stop doing it entirely, but it's become pretty normal to both keep the string null-terminated for compatibility reasons, and also explicitly keep track of its length. More on reddit.com
๐ŸŒ r/computerscience
20
29
February 2, 2023
java - Why do I have a null character in my output? - Stack Overflow
I split the string so that I can work with each individual word. It seems like the easiest method to me. ... @StephenC You're right! Null character would be nothing there. But those four characters were still pretty annoying. ... That's because the default value of an object in Java is Null. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 5
91

Does replacing a character in a String with a null character even work in Java? I know that '\0' will terminate a c-string.

That depends on how you define what is working. Does it replace all occurrences of the target character with '\0'? Absolutely!

CopyString s = "food".replace('o', '\0');
System.out.println(s.indexOf('\0')); // "1"
System.out.println(s.indexOf('d')); // "3"
System.out.println(s.length()); // "4"
System.out.println(s.hashCode() == 'f'*31*31*31 + 'd'); // "true"

Everything seems to work fine to me! indexOf can find it, it counts as part of the length, and its value for hash code calculation is 0; everything is as specified by the JLS/API.

It DOESN'T work if you expect replacing a character with the null character would somehow remove that character from the string. Of course it doesn't work like that. A null character is still a character!

CopyString s = Character.toString('\0');
System.out.println(s.length()); // "1"
assert s.charAt(0) == 0;

It also DOESN'T work if you expect the null character to terminate a string. It's evident from the snippets above, but it's also clearly specified in JLS (10.9. An Array of Characters is Not a String):

In the Java programming language, unlike C, an array of char is not a String, and neither a String nor an array of char is terminated by '\u0000' (the NUL character).


Would this be the culprit to the funky characters?

Now we're talking about an entirely different thing, i.e. how the string is rendered on screen. Truth is, even "Hello world!" will look funky if you use dingbats font. A unicode string may look funky in one locale but not the other. Even a properly rendered unicode string containing, say, Chinese characters, may still look funky to someone from, say, Greenland.

That said, the null character probably will look funky regardless; usually it's not a character that you want to display. That said, since null character is not the string terminator, Java is more than capable of handling it one way or another.


Now to address what we assume is the intended effect, i.e. remove all period from a string, the simplest solution is to use the replace(CharSequence, CharSequence) overload.

CopySystem.out.println("A.E.I.O.U".replace(".", "")); // AEIOU

The replaceAll solution is mentioned here too, but that works with regular expression, which is why you need to escape the dot meta character, and is likely to be slower.

2 of 5
7

Should be probably changed to

CopyfirstName = firstName.trim().replaceAll("\\.", "");
๐ŸŒ
Oracle
docs.oracle.com โ€บ javaee โ€บ 7 โ€บ tutorial โ€บ bean-validation002.htm
21.2 Validating Null and Empty Strings - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "". It is a character sequence of zero characters. A null string is represented by null.
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2021 โ€บ 11 โ€บ null-character-java.html
null character java | Java Hungry
'\u0000' is the lowest range of the Unicode system used by Java. public class EmptyCharacterExample2 { public static void main(String args[]) { // Assign null character to ch variable char ch = '\u0000'; String givenString = "Be in present"; // Replacing 'e' in the givenString with null character String result = givenString.replace('e',ch); // Printing the result string System.out.println("Replace givenString with null char: " + result); } } Output: Replace givenString with null char: B in pr s nt
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ threads โ€บ 425629 โ€บ how-to-check-null-character-in-an-array
java - How to check NULL character in an array [SOLVED] | DaniWeb
June 14, 2012 - I think the OP is referring to the C convention of representing strings as arrays of characters with a zero at the end. โ€” JamesCherrill 4,733 Jump to Post ยท There's no such entity as NULL in Java. There is null, but that is not a valid value ...
๐ŸŒ
SSOJet
ssojet.com โ€บ special-characters โ€บ null-0-in-java
Null (\0) in Java | Special Characters in Programming Languages
The null character, denoted by \0, is a control character with an ASCII value of 0. While it serves as a string terminator in C-style programming, Java's String objects manage their length internally and don't depend on this convention.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ null character '\0' & null terminated strings
r/C_Programming on Reddit: Null character '\0' & null terminated strings
December 25, 2022 -

Hello everyone!
In C, strings (character arrays) are terminated by null character '\0' - character with value zero.
In ASCII, the NUL control code has value 0 (0x00). Now, if we were working in different character set (say the machine's character set wouldn't be ASCII but different one), should the strings be terminated by NUL in that character set, or by a character whose value is zero?

For example, if the machine's character set would be UTF-16, the in C, byte would be 16bits and strings would be terminated by \0 character with value 0x00 00, which is also NUL in UTF-16.
But, what if the machine's character set would be modified UTF-8 (or UTF-7, ...). Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. How would be strings terminated in that case? By the byte with value 0 or by the null character.

I guess my question could be rephrased as: Are null terminated strings terminated by the NUL character (which in that character set might be represented by a nonzero value) or by a character whose value is zero (which in that character set might not represent the NUL character).

Thank you all very much and I'm sorry for all mistakes and errors as english is not my first language.

Thanks again.

Top answer
1 of 3
31
should the strings be terminated by NUL in that character set, or by a character whose value is zero? The character '\0' is guaranteed to be a byte with all bits zero, and to have a numeric value equal to zero. A string in C always ends with this character. Then, according to Wikipedia, the null character is encoded as two bytes 0xC0, 0x80. No, in standard UTF-8 the code point with value zero is encoded in a single zero byte. You may have been reading something about "modified UTF-8", which appears to be a rather Java-centric external encoding for strings. It deliberately uses an "overlong" encoding of Java '\u0000' so that the resulting byte sequence does not contain a zero byte. One reason for this is because the length of strings in Java is not defined by use of a terminating character โ€” a Java string can contain arbitrary '\u0000' characters โ€” and you might need some way to round-trip such strings between Java and a language like C that does use a zero byte as a terminator.
2 of 3
17
C11 states: 5.2 Environmental considerations 5.2.1 Character sets 2. In a character constant or string literal, members of the execution character set shall be represented by corresponding members of the source character set or by escape sequences consisting of the backslash \ followed by one or more characters. A byte with all bits set to 0, called the null character, shall exist in the basic execution character set; it is used to terminate a character string. Emphasis is mine From that we can understand that the terminating null character is always completely 0. Then, there's: 5.2.1.2 Multibyte characters A byte with all bits zero shall be interpreted as a null character independent of shift state. Such a byte shall not occur as part of any other multibyte character. 7.1.1 Definitions of terms A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.
๐ŸŒ
Quora
quora.com โ€บ Are-Java-strings-null-terminated
Are Java strings null terminated? - Quora
Answer (1 of 3): Java strings internally uses 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.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ strings-null-terminated-java-shivani-goel
Strings are not null terminated in Java.
September 16, 2019 - Unlike C and C++, String in Java doesn't terminate with null character. Instead String are Object in Java and backed by character array.
๐ŸŒ
Reddit
reddit.com โ€บ r/computerscience โ€บ is a null character really the most efficient way to mark the end of a string in memory?
r/computerscience on Reddit: Is a null character really the most efficient way to mark the end of a string in memory?
February 2, 2023 -

I'm very new to CS50 and I don't get why there's no possible alternative, intuitively with almost no knowledge it seems like you could have one byte represent multiple separations and all you'd need to to is preallocate a bit of memory for an extra function that rewrites the bytes. Would that use more memory than it saves? Is it problematic to store multiple separations in one byte?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ does-java-define-string-as-null-terminated
Does Java Define String as Null Terminated? - GeeksforGeeks
July 23, 2025 - String: Hello Length: 5 Java strings do not have a null terminator! In Java, strings are represented as immutable object i.e., they cannot be modified after creation. They are handled by the JVM (Java Virtual Machine), and internally represented ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 55203183 โ€บ why-do-i-have-a-null-character-in-my-output โ€บ 55203211
java - Why do I have a null character in my output? - Stack Overflow
I split the string so that I can work with each individual word. It seems like the easiest method to me. ... @StephenC You're right! Null character would be nothing there. But those four characters were still pretty annoying. ... That's because the default value of an object in Java is Null.
๐ŸŒ
MojoAuth
mojoauth.com โ€บ special-characters โ€บ null-0-in-java
Null (\0) in Java | Understanding Special Characters in Programming
The null character \0 in Java is treated as a regular character that can be included in strings. It is commonly used to represent a terminating character in various programming scenarios, especially when dealing with byte streams or raw data.
๐ŸŒ
Quora
quora.com โ€บ Do-strings-in-Java-also-terminate-with-a-0-character-like-strings-in-C
Do strings in Java also terminate with a '\0' character like strings in C? - Quora
Answer (1 of 5): In C there is way to track the end of the string for which which we use '\0' character to denote the end of string. But when it comes to Java, String is class defined in java.lang package. All the String are instantiated using it. String class tracks the length of strings which ...
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_character
Null character - Wikipedia
March 3, 2026 - Since the null character is not a printable character, representing it requires special notation in source code. In a string literal, the null character is often represented as the escape sequence \0 (for example, "abc\0def"). Similar notation ...
๐ŸŒ
Medium
medium.com โ€บ @ecetasci.iu โ€บ checking-for-null-or-empty-strings-in-java-19518fa1e553
Checking for Null or Empty Strings in Java | by Ece Tasci | Medium
February 25, 2025 - One of the most common ways to ensure a String is valid before using it is by checking if(name != null && !name.isEmpty()). This simple condition helps prevent NullPointerException and ensures that the String contains meaningful data. Introduced in Java 6, the isEmpty() method is part of the String class and returns true if the String has zero characters.
๐ŸŒ
Medium
medium.com โ€บ @javacharter โ€บ can-you-check-if-char-is-null-or-not-in-java-9f5adcc51f77
Can you check if char is null or not in Java ? | by Javacharter | Medium
April 12, 2023 - Simplest approach I could think ... the code, I have put one condition if(ch!=0) ! Yes that is true! A char in Java cannot be null as it is a primitive ....