You may assign '\u0000' (or 0). For this purpose, use Character.MIN_VALUE.

Character ch = Character.MIN_VALUE;
Answer from KV Prajapati on Stack Overflow
🌐
Coderanch
coderanch.com › t › 730461 › java › null-null-character
null? null character? (Beginning Java forum at Coderanch)
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
🌐
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.
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("\\.", "");
🌐
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
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 ...
🌐
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
🌐
Reddit
reddit.com › r/c_programming › null character '\0' & null terminated strings
r/C_Programming on Reddit: Null character '\0' & null terminated strings
September 7, 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.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 656159 › java › Check-character-null
Check if character is null (Beginning Java forum at Coderanch)
A String object might hide a char[] like this:- ['P', 'i', 'e', 'p', 's', ' ', 'W', 'i', 'l', 'l', 'e', 'm', 's', 'e', 'n'] Note the array · does not end with a null character or anything. Its last character is 'n'. By the way, Java® chars are different from in C, too.
🌐
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.
🌐
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.
🌐
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.
🌐
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
January 7, 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 ...
🌐
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?

🌐
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.
🌐
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 ....