You may assign '\u0000' (or 0).
For this purpose, use Character.MIN_VALUE.
Character ch = Character.MIN_VALUE;
Answer from KV Prajapati on Stack OverflowYou may assign '\u0000' (or 0).
For this purpose, use Character.MIN_VALUE.
Character ch = Character.MIN_VALUE;
char means exactly one character. You can't assign zero characters to this type.
That means that there is no char value for which String.replace(char, char) would return a string with a diffrent length.
Java string replace and the NUL (NULL, ASCII 0) character? - Stack Overflow
What does '\0' mean in java?
java - Why do I have a null character in my output? - Stack Overflow
java - what happens when assigning a null character? - Stack Overflow
How do I write a null character in Java?
How do I remove null characters from a string in Java?
Does Java use null-terminated strings?
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
charis not aString, and neither aStringnor an array ofcharis 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.
Should be probably changed to
firstName = firstName.trim().replaceAll("\\.", "");
In C you are basically setting the pointer to zero value. In fact
char *c = NULL;
is equivalent to
char *c = 0;
Zero pointer cannot be dereferenced since there is no memory mapping for this address. On platforms with virtual memory, an attempt to do so triggers a page fault transferring control to the operating system which then usually handles the situation by killing the offending process (in UNIX your process receives Segmentation Violation signal SIGSEGV).
In Java, all non-primitive type variables are references. null in Java is a literal denoting the only value of a special unnamed type. This type can be cast to any reference type allowing you to put null into any reference variable. An attempt to use such reference to access an object will throw an unchecked exception called NullPointerException. See JLS 3.10.7 and JLS 4.1 for details.
What you do here is assign a char pointer to NULL.
It has basically the same effect, with the huge difference that dereferencing NULL in C will make your program die with a SIGSEGV (well, under Unix-like OSes, including Linux and Mac OS X), while Java will throw a NullPointerException.
Note that mc is really a reference, not a class by itself. It is nearly the same as a C pointer.
NULL has some practical value in both cases anyway: you can test if (mc == null) in Java. In C, that would be a simple if (!p) (but if (p == NULL) also works).