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 (' ').

Answer from Nikita Rybak on Stack Overflow
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.

🌐
Tutorialspoint
tutorialspoint.com › java › lang › character_iswhitespace.htm
Java - Character isWhitespace() Method
Unicode space character ... parameters but same return type. Following is the syntax for Java Character isWhitespace() method...
🌐
LabEx
labex.io › tutorials › java-determining-space-characters-in-java-117547
Java Programming Tutorials | Space Character Identification | LabEx
In Java, the Character class provides ... is a space character. Space characters include standard space (U+0020), as well as other whitespace characters like line breaks and tabs....
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Character.html
Character (Java Platform SE 8 )
March 16, 2026 - A character is a Java whitespace character if and only if it satisfies one of the following criteria: It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
🌐
Coderanch
coderanch.com › t › 384602 › java › character-representation-blank-space
character representation of a blank space (Java in General forum at Coderanch)
The resulting string should be like this "28764" (note:there should not be any space between). Thanks, Vinod This link will answer your question. ... I have read the documentation. There is only one method for replacing. i.e replace(char,char). How can i get the same result as this: String oldStr="2,756"; oldStr=oldStr.replaceAll(",",""); using the replace(char,char) method of java 1.3 Thanks, Vinod
🌐
QBasic on Your Computer
chortle.ccsu.edu › finiteautomata › Section07 › sect07_6.html
Space Character
A string can begin (and can end) with one or several space characters. For example, here is a regular expression that starts with two spaces (this might be hard to see): ... The quote marks are not part of the regular expression. They are used here to show the leading spaces. (However, in a Java program, a regular expression is given as a String literal and must be surrounded by quote marks.)
🌐
GeeksforGeeks
geeksforgeeks.org › java › character-iswhitespace-method-in-java-with-examples
Character.isWhitespace() method in Java with examples - GeeksforGeeks
December 6, 2018 - A character is a Java whitespace ... criteria: It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F')....
🌐
Coderanch
coderanch.com › t › 416932 › java › char-space-String-space
char ' ' space vs. String space (Beginning Java forum at Coderanch)
Say I have a String type variable and concatenate a blank space, I could use: String word = aWord + " ";. I would guess it has to do with char being a primitive type variable and String a reference variable?
🌐
Javatpoint
javatpoint.com › post › java-character-iswhitespace-method
Java Character isWhitespace() Method - Javatpoint
The isWhitespace(char ch) method of Character class determines whether the given character(Unicode code point) is a whitespace character or not · A character in Java can be considered as a whitespace character if one of the following criteria is satisfied:
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Character.html
Character (Java Platform SE 7 )
A character is a Java whitespace character if and only if it satisfies one of the following criteria: It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
🌐
Educative
educative.io › answers › what-is-characteriswhitespace-in-java
What is Character.isWhitespace() in Java?
The isWhitespace() function returns true if the character sent as a parameter is whitespace and returns false otherwise. class JAVA { public static void main( String args[] ) { //simple letter ·
🌐
Javatpoint
javatpoint.com › post › java-character-isspace-method
Java Character isSpace() Method - Javatpoint
The isSpace(char ch)method of Character class determines whether the given(or specified) character is ISO-LATIN-1 white space · The given method returns true for the following five characters:
🌐
LabEx
labex.io › tutorials › java-exploring-java-character-space-detection-117545
Java Programming | Character Class | Space Char Identification | LabEx
Learn how to use the isSpaceChar(char ch) method in Java to determine if a character is a Unicode space character. Hands-on coding examples provided.
🌐
Mathbits
mathbits.com › JavaBitsNotebook › LibraryMethods › CharacterMethods.html
Java Character Class and Methods - JavaBitsNotebook.com
We have worked with characters (char) as primitive data types. Now, we are ready to examine the Character class which offers a number of useful methods for manipulating characters · Character is located in the java.lang package. The full class name is java.lang.Character.
🌐
LabEx
labex.io › tutorials › java-how-to-identify-different-types-of-space-characters-using-isspacechar-in-java-417398
How to identify different types of space characters using isSpaceChar() in Java | LabEx
Understanding the different types of space characters is essential for tasks such as text parsing, string manipulation, and input validation. Java provides the isSpaceChar() method, which is part of the Character class, to help identify different types of space characters.
🌐
TutorialsPoint
tutorialspoint.com › check-whether-the-entered-value-is-whitespace-or-not-in-java
Check whether the entered value is whitespace or not in Java
To check whether the entered value is whitespace or not in Java, use the Character.isWhitespace() method. We have a value to be checked. char val = ' '; Now let us use the Character.isWhitespace() method.
🌐
Studytonight
studytonight.com › java-wrapper-class › java-character-iswhitepsacechar-ch-method
Java Character isWhitepsace(char ch) Method - Studytonight
November 24, 2020 - A character is a Java whitespace ... criteria: It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F')....