You can use c[i]= '\0' or simply c[i] = (char) 0.
The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.
You can't store "no character" in a character - it doesn't make sense.
As an alternative you could store a character that has a special meaning to you - e.g. null char '\0' - and treat this specially.
java - How do I compare a character to check if it is null? - Stack Overflow
the null character - C++ Forum
NULL character in C
java - What's the default value of char? - Stack Overflow
What is a null character?
How does the null character relate to the concept of 'null' in other programming contexts?
Does every programming language use the null character for string termination?
Videos
A char cannot be null as it is a primitive so you cannot check if it equals null, you will have to find a workaround.
Also did you want to check if letterChar == ' ' a space or an empty string? Since you have a space there.
The first two answers here may be helpful for how you can either check if String letter is null first.
or cast char letterChar into an int and check if it equals 0 since the default value of a char is \u0000- (the nul character on the ascii table, not the null reference which is what you are checking for when you say letterChar == null)- which when cast will be 0.
char is a primitive datatype so can not be used to check null.
For your case you can check like this.
if (letterChar == 0) //will be checked implicitly
{
System.out.println("null");
}
//or use this
if (letterChar == '\0')
{
System.out.println("null");
}
Check that the String s is not null before doing any character checks. The characters returned by String#charAt are primitive char types and will never be null:
Copyif (s != null) {
...
If you're trying to process characters from String one at a time, you can use:
Copyfor (char c: s.toCharArray()) {
// do stuff with char c
}
(Unlike C, NULL terminator checking is not done in Java.)
Default value to char primitives is 0 , as its ascii value. you can check char if it is null. for eg:
Copychar ch[] = new char[20]; //here the whole array will be initialized with '\u0000' i.e. 0
if((int)ch[0]==0){
System.out.println("char is null");
}
So, it's represented by '\0' or '0', right?
Why '0' too when it's value is 48 and not 0 - is it not the character '0' in ASCII but the symbol '0' used when talking about the NULL character?
EDIT: So it's just the ASCII character '\0'.
ALSO: Why is a char array printable as a string even if it contains no NULL character?
#include <stdio.h>
int main(void)
{
char s[] = {'h', 'i', '.', '.'};
printf("%s\n", s);
printf("%i %i %i %i\n", s[0], s[1], s[2], s[3]);
}prints
hi.. 104 105 46 46
but if I change the last line to
printf("%i %i %i %i %i\n", s[0], s[1], s[2], s[3], s[4);it complains "array index 4 is past the end of the array" which means there is no NULL.
EDIT: I just used the code above and changed s[] to s[5] and it printed the additional 0 - why - can't printf see the NULL if you declare the array without specifying its size in advance?
The default value of a char attribute is indeed '\u0000' (the null character) as stated in the Java Language Specification, section ยง4.12.5 Initial Values of Variables .
In my system, the line System.out.println('\u0000'); prints a little square, meaning that it's not a printable character - as expected.
'\u0000' is the default value for a character. Its decimal equivalent is 0.
When you are declaring some char variable without initializing it, '\u0000' will be assigned to it by default.
see this code
public class Test {
char c;
public static void main(String args[]) throws Exception {
Test t = new Test();
char c1 = '\u0000';
System.out.println(t.c);
System.out.println(c1);
System.out.println(t.c == c1);
}
}
This code will print true for the last print.