Let's take arrays out of the equation - an array is just a collection of variables, really. Let's consider a single variable. Suppose we had a method like this:

public boolean isEmpty(char c)

What would that do? The value of c cannot be null, because char is a primitive type. It could be U+0000 (aka '\u0000' or '\0' as character literals in Java), and that's the default value of char (for array elements and fields) but it's not the same as a null reference.

If you want a type which is like char but is a reference type, you should consider using Character - the wrapper type for char just like Integer is for int. Or if you know that you'll never use U+0000 as a valid value, you could just stick with that.

However, a better alternative would often be to design your code so that you don't need the concept of "empty or not empty". We don't know what you're trying to achieve here, but it's usually a good thing to at least consider. For arrays, the alternative is often to use an ArrayList<E> - but of course you can't have an ArrayList<char> in Java as Java generics don't allow primitive type arguments :(

Answer from Jon Skeet on Stack Overflow
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 275722 › check-if-char-array-is-empty
java - Check if char array is empty | DaniWeb
You can only refer to array indexes with integers, not ' ' as you have done. To see if an array is completely empty, you need to use a for loop. I believe array elements are automatically initialized to null, so if you want to check if some ...
Discussions

Android JAVA how to check if some index of char array is empty - Stack Overflow
I m working with an array of characters in java. The array can have some missing entries in the middle and I need to know the leftmost index that is empty My code for that is private int More on stackoverflow.com
🌐 stackoverflow.com
How to check if a char array is blank (or at least 20 ?)<< SOLVED>>
This is a small test sketch : I only want to print these lines if they contain anything: Would anyone please assist me. Many thanks in advance. char line0 [19]; char line1 [19]; char line2 [19]; char line3 [19]; void setup() { Serial.begin(9600); while (!Serial); // wait for serial connection ... More on forum.arduino.cc
🌐 forum.arduino.cc
19
0
January 21, 2022
java - Checking if char is empty - Stack Overflow
I have a small problem converting code from java to C++ I am trying to check if a 2d array is set ,this is my java code for(int area_y = y -1 ;area_y > 0 ;area_y--) { for(int are... More on stackoverflow.com
🌐 stackoverflow.com
February 13, 2013
java - How to check if a char is equal to an empty space? - Stack Overflow
I remember Java having a comparer like this... Any suggestions? ... Also, by using double quotes you create String constant (" "), while with single quotes it's a char constant (' '). ... Sign up to request clarification or add additional context in comments. ... The code you needs depends on what you mean by "an empty space". If ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Stack Overflow
stackoverflow.com › questions › 22639080 › android-java-how-to-check-if-some-index-of-char-array-is-empty
Android JAVA how to check if some index of char array is empty - Stack Overflow
I m working with an array of characters in java. The array can have some missing entries in the middle and I need to know the leftmost index that is empty ... private int checkMissingEntry(){ int p = 0; for(int i = characterArray.length - 1; i >= 0; i--){ if (characterArray[i] == ' '){ p = i; } else{ } } return p; }
🌐
Quora
quora.com › How-do-I-check-if-a-char-array-is-empty
How to check if a char array is empty - Quora
Answer (1 of 10): Alan gave good answer but let me extend a bit. What empty char array means? Let me use C++ array (vector) class to explain. [code]std::vector String; [/code]In C world this is something like: [code]char* pString = NULL; // non allocated, zero sized array pString = (c...
🌐
Arduino Forum
forum.arduino.cc › projects › programming
How to check if a char array is blank (or at least 20 ?)<< SOLVED>> - Programming - Arduino Forum
January 21, 2022 - This is a small test sketch : I only want to print these lines if they contain anything: Would anyone please assist me. Many thanks in advance. char line0 [19]; char line1 [19]; char line2 [19]; char line3 [19]; void setup() { Serial.begin(9600); while (!Serial); // wait for serial connection ...
🌐
Baeldung
baeldung.com › home › java › java array › checking if an array is null or empty in java
Checking if an Array Is Null or Empty in Java | Baeldung
July 16, 2024 - Checking if an array is null or empty is a fundamental task in Java programming. In this article, we’ve explored how to implement these checks for both object and primitive type arrays, creating a robust utility method that can be reused in our applications.
🌐
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 - If your data originates from C as bytes, consider scanning the byte array for 0 first, then decode only the meaningful slice to a String. In Java char is a 16 bit unsigned numeric value, so zero is perfectly valid anywhere, but the Java equivalent of your loop wold be something like for (int i = 0; my-char-array[i] != 0; i++) {... of course that will give an array index out of bounds if the array …
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › how to check whether an array is null empty
How to Check Whether an Array Is Null/Empty in Java | Delft Stack
February 2, 2024 - import org.apache.commons.lang... you are working with Java 8 or higher version then you can use the stream() method of Arrays class to call the allMatch() method to check whether array contains null values or not...
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-an-array-is-empty-or-not-in-java
How to Check if an Array is Empty or Not in Java
August 6, 2024 - After checking if it is empty or not the result will be ? ... Step 1 ? Declare and initialize an integer array. Step 2 ? Get the length of the array. Step 3 ? If length is equal to 0 then array is empty otherwise not.
🌐
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 - If you go through 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 . If you put a null check, it will give you compile time error.
🌐
Silicon Cloud
silicloud.com › home › how to determine if a char array is empty in c language?
How to determine if a char array is empty in C language? - Blog - Silicon Cloud
March 28, 2024 - In the above code, the isCharArrayEmpty function is used to check if a char array is empty. In the main function, the isCharArrayEmpty function is called to determine if the two arrays arr1 and arr2 are empty. ... How can we use strlen in C language to calculate the length of an array?(Opens ...
🌐
Coderanch
coderanch.com › t › 656159 › java › Check-character-null
Check if character is null (Beginning Java forum at Coderanch)
October 3, 2015 - When you print out the first character in that array with the printf function the chip display the ASCII equivalent of each number, until it reaches the 00 which does not print. The 00 is called · the null character and that style of String is called null‑terminated Strings or zero#x2011;terminated Strings. So I am actually writing about what you are interested in. Things are totally different in Java®. A String is a full‑blown object and it incorporates a char[]. Remember that in Java® arrays are full‑blown objects too.
🌐
Quora
quora.com › How-do-I-check-if-array-is-empty-or-not
How to check if array is empty or not - Quora
... Seth D. Fulmer · 20+ years ... the language - In Java it’s simple: Assuming you have an array called arr, simply test arr.length for > 0....
Top answer
1 of 11
247
if (c == ' ')

char is a primitive data type, so it can be compared with ==.

Also, by using double quotes you create String constant (" "), while with single quotes it's a char constant (' ').

2 of 11
91

The code you needs depends on what you mean by "an empty space".

  • If you mean the ASCII / Latin-1 / Unicode space character (0x20) aka SP, then:

    if (ch == ' ') {
        // ...
    }
    
  • If you mean any of the traditional ASCII whitespace characters (SP, HT, VT, CR, NL), then:

    if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\x0b') {
        // ...
    }
    
  • If you mean any Unicode whitespace character, then:

    if (Character.isWhitespace(ch)) {
        // ...
    }
    

Note that there are Unicode whitespace includes additional ASCII control codes, and some other Unicode characters in higher code planes; see the javadoc for Character.isWhitespace(char).


What you wrote was this:

    if (Equals(ch, " ")) {
        // ...
    }

This is wrong on a number of levels. Firstly, the way that the Java compiler tries to interpret that is as a call to a method with a signature of boolean Equals(char, String).

  • This is wrong because no method exists, as the compiler reported in the error message.
  • Equals wouldn't normally be the name of a method anyway. The Java convention is that method names start with a lower case letter.
  • Your code (as written) was trying to compare a character and a String, but char and String are not comparable and cannot be cast to a common base type.

There is such a thing as a Comparator in Java, but it is an interface not a method, and it is declared like this:

    public interface Comparator<T> {
        public int compare(T v1, T v2);
    }

In other words, the method name is compare (not Equals), it returns an integer (not a boolean), and it compares two values that can be promoted to the type given by the type parameter.


Someone (in a deleted Answer!) said they tried this:

    if (c == " ")

That fails for two reasons:

  • " " is a String literal and not a character literal, and Java does not allow direct comparison of String and char values.

  • You should NEVER compare Strings or String literals using ==. The == operator on a reference type compares object identity, not object value. In the case of String it is common to have different objects with different identity and the same value. An == test will often give the wrong answer ... from the perspective of what you are trying to do here.

🌐
Quora
quora.com › How-can-I-set-a-character-array-to-null-in-Java-Or-empty-it
How to set a character array to null in Java? Or empty it - Quora
Answer (1 of 6): [code]class EmptyCharArray{ public static void main(string[] args) { char namearray[6] = {'a', 'n', 'm', 'o', 'l'}; for(int i=0;i
🌐
Delft Stack
delftstack.com › home › howto › java › 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 ...