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
https://coderanch.com › t › 730461 › java › null-null-character
null? null character? (Beginning Java forum at Coderanch)
May 14, 2020 - The concept of reference is a central concept in an object-oriented language such as Java. If you haven't heard of references then I'm not going to explain them here, but hopefully you find out soon. ... Well, when someone speak of such a thing as a "null character" it's often what's called a "zero- (\0) terminated String" which is more a part of languages that do not see a String as a fixed length sequence of characters but rather just a bunch of values in a row terminated by single byte which value is 0x00.
Discussions

Java string replace and the NUL (NULL, ASCII 0) character? - Stack Overflow
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 ... More on stackoverflow.com
🌐 stackoverflow.com
What does '\0' mean in java?
It's zero, not null :) It's a char with ascii value zero, as opposed to the number zero. More on reddit.com
🌐 r/learnjava
7
2
February 10, 2021
java - Why do I have a null character in my output? - Stack Overflow
So, I tried making a program that's supposed to take input for a String from the user and invert each String, somewhat like this for example: stack -> kcats. My program works almost fine, except th... More on stackoverflow.com
🌐 stackoverflow.com
java - what happens when assigning a null character? - Stack Overflow
In Java, you can't say "null character". More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How do I write a null character in Java?
Write '\0' or '\u0000' as a char literal, or "\0" in a string. All three produce code point 0.
🌐
mojoauth.com
mojoauth.com › dev guides › null character in java: the \0 char explained
Null Character in Java: The \0 char Explained | MojoAuth Dev Guides
How do I remove null characters from a string in Java?
Use s.replace("\0", ""). To keep only the part before the first NUL, use s.substring(0, s.indexOf('\0')) after checking that indexOf is not -1.
🌐
mojoauth.com
mojoauth.com › dev guides › null character in java: the \0 char explained
Null Character in Java: The \0 char Explained | MojoAuth Dev Guides
Does Java use null-terminated strings?
No. Java strings store their length, so \0 is an ordinary character and does not end the string.
🌐
mojoauth.com
mojoauth.com › dev guides › null character in java: the \0 char explained
Null Character in Java: The \0 char Explained | MojoAuth Dev Guides
🌐
MojoAuth
https://mojoauth.com › dev guides › null character in java: the \0 char explained
Null Character in Java: The \0 char Explained | MojoAuth Dev Guides
September 28, 2026 - Use the null character in Java: write '\0' or '\u0000', check for it in strings, and see how the Java null char differs from null references.
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!

String 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!

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

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

firstName = firstName.trim().replaceAll("\\.", "");
🌐
Blogger
https://javahungry.blogspot.com › 2021 › 11 › null-character-java.html
null character java | Java Hungry
There are 3 ways through which we can represent the null or empty character in Java. 1. Using Character.MIN_VALUE 2. Using '\u0000' 3.
🌐
SSOJet
https://ssojet.com › dev guides › special characters › null (\0) in java
Null (\0) in Java | Developer Guides
November 19, 2025 - You’ll learn how to use Optional to avoid NullPointerExceptions, express intent more clearly, and write more robust, readable Java code. The null character, denoted by \0, is a control character with an ASCII value of 0.
🌐
Codemia
https://codemia.io › home › knowledge hub › how to represent empty char in java character class
How to represent empty char in Java Character class | Codemia
July 31, 2025 - However, in Java, the concept of an empty character varies slightly due to its type system and Unicode representation. ... The null character, represented by the Unicode '\u0000', is the default value for an uninitialized char in Java.
Find elsewhere
🌐
MojoAuth
https://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.
🌐
DaniWeb
https://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.
🌐
Compile7
https://compile7.org › special-characters › how-to-use-null-0-in-java
How to use Null (\0) in Java | Understanding Special Characters in Programming
In Java, the null character is represented by the escape sequence \0. This special character corresponds to the ASCII value of 0.
🌐
javathinking
https://javathinking.com › blog › how-do-i-compare-a-character-to-check-if-it-is-null
How to Check if a Character is Null in Java: Why `s.charAt(j) == null` Throws an Error (Correct Method) — javathinking.com
The term "null character" often refers to the Unicode character '\u0000' (also called the "null terminator"). This is a valid char value, not a null reference. It is sometimes used as a placeholder or terminator in low-level programming (e.g., C-style strings), but in Java, strings are not null-terminated—they are length-prefixed.
🌐
Medium
https://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 - “The operator != is undefined for the argument type(s) char, null” · So workaround for that is to check using ‘0’. Is this correct ? What are your thoughts about this ? Please write down in the comment section . ... At Java Charter you can get evolve with Java and technologies like Spring , Spring Boot , REST APIs, Microservices, Various Tools, Azure Cloud etc…
🌐
Reddit
https://reddit.com › r/learnjava › what does '\0' mean in java?
What does '\0' mean in java? : r/learnjava
February 10, 2021 - It's the 0th position in the ASCII table which is called NUL. You'll notice that the first 32 characters are control characters for reasons of ancient history. \0 represents null in pretty much every language I have used. For Java characters you can also use the Unicode '\u0000' and Java's ...
🌐
Delft Stack
https://delftstack.com › "delft stack" › "howto" › "java howtos" › "how to represent empty char in java"
How to Represent Empty Char in Java | Delft Stack
February 2, 2024 - Character Class and \0 Constant: The Character class with \0 allows for direct empty character representation, useful for precision in primitive char type control. Unicode Null Character: Represented as \u0000, it offers cross-language compatibility ...
🌐
GeeksforGeeks
https://geeksforgeeks.org › java › interesting-facts-about-null-in-java
Interesting facts about null in Java - GeeksforGeeks
September 3, 2024 - In Java, null is a special value that represents the absence of a value or reference. It is used to indicate that a variable or object does not currently have a value assigned to it.
🌐
Logit
https://logit.io › home › blog › the concept of null in java
The Concept Of Null In Java
February 4, 2025 - Null is a reserved word (keyword) in Java for literal values. It is a literal similar to the true and false. In Java, null is a keyword much like the other keywords public, static or final.
🌐
Stack Overflow
https://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
@StephenC You're right! Null character would be nothing there. But those four characters were still pretty annoying. ... Save this answer. ... Show activity on this post. That's because the default value of an object in Java is Null. Here, the String array index value (i.e r[i]) initially is null.
🌐
CodingTechRoom
https://codingtechroom.com › question › -null-characters-in-java-strings
How to Handle Null Characters in Java Strings? - CodingTechRoom
In Java, a null character (represented as '\u0000') is the first character in the Unicode character set. This character is often used as a string terminator in languages like C, but in Java, its presence in strings can lead to confusion as it ...