Perhaps, this will help to understand the difference on == vs equals.

  @Test
  public void testCharacterEquals() {
    //primitive type uses == operator for equals comparasion
    char a1 = 'A';
    char a2 = 'A';
    if (a1 == a2) {
      System.out.println("primitive type comparasion: it's equal");
    }

    //From Java doc; The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.
    //Object type uses equals method for equals comparasion
    Character character1 = 'A';
    Character character2 = 'A';
    if (character1.equals(character2)) {
      System.out.println("object type comparasion: it's equal");
    }
  }
Answer from Pradip Karki on Stack Overflow
🌐
Reddit
reddit.com › r/javahelp › char .equals() method
r/javahelp on Reddit: Char .equals() method
January 15, 2023 -

Hello!

I'm a beginner java coder and I'm trying to write a code that reads a string character by character. The string consists of multiple words separated by a space, and I need it to separate that one long string into multiple shorter strings that consists of each individual word. This is the code I have so far:

	for (int i = 0; i < userinput.length(); i++) {

		if ((userinput.charAt(i)).equals(' ')) {

//Code methods here

		}

	}

I'm receiving an error saying "Cannot invoke equals(char) on the primitive type char" on the if line. Any help?

Thanks!

🌐
GeeksforGeeks
geeksforgeeks.org › java › character-equals-method-in-java-with-examples
Character.equals() method in Java with examples - GeeksforGeeks
December 6, 2018 - The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object.
Top answer
1 of 5
6

Perhaps, this will help to understand the difference on == vs equals.

  @Test
  public void testCharacterEquals() {
    //primitive type uses == operator for equals comparasion
    char a1 = 'A';
    char a2 = 'A';
    if (a1 == a2) {
      System.out.println("primitive type comparasion: it's equal");
    }

    //From Java doc; The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.
    //Object type uses equals method for equals comparasion
    Character character1 = 'A';
    Character character2 = 'A';
    if (character1.equals(character2)) {
      System.out.println("object type comparasion: it's equal");
    }
  }
2 of 5
0

It depends on using a primitive type, char, int, etc. And using Objects like String. A primitive type like an int can be compared 1 == 1 and if you check 2 objects to each other ObjectA != ObjectB.

Check out this answer over here: Primitive vs Object type in Java Or over here: https://chortle.ccsu.edu/java5/Notes/chap09C/ch09C_2.html

Quote:

A primitive data type uses a small amount of memory to represent a single item of data. All data of the same primitive type are the same size.

For example, primitive type int represents integers using 32 bits. All variables of type int use 32 bits.

There are only eight primitive data types in Java: byte, short, int, long, float, double, char, and boolean. A Java program cannot define any other primitive data types.

An object is a large chunk of memory that can potentially contain a great deal of data along with methods (little programs) to process that data. There are thousands of object classes that come standard with Java, and a programmer can easily create additional classes. (Although there are thousands of standard classes, for this course you only need become familiar with a dozen or so classes.)

Where 2 strings are 2 different objects. Therefor not the same object and not the same string. While the characters might be the same.

🌐
Tutorialspoint
tutorialspoint.com › java › lang › character_equals.htm
Java - Character equals() Method
The Java Character equals() is used to compare two Character objects against each other. The equals() method accepts a Character object as an argument, compares it with another Character object the method is invoked on, then returns a boolean value
🌐
Stanford
web.stanford.edu › class › archive › cs › cs108 › cs108.1082 › 106a-java-handouts › HO38Strings.pdf pdf
CS106A, Stanford Handout #38 Fall, 2004-05 Nick Parlante Strings and Chars
Use equals() for objects, use == for primitives. ... The Java String class implement many useful methods. int length() – returns the number of chars in the receiver string.
🌐
Baeldung
baeldung.com › home › java › core java › compare characters in java
Compare Characters in Java | Baeldung
December 26, 2024 - It denotes the difference between the ASCII code of a and b. The returned value is equal to zero if the two char values are identical, less than zero if a < b, and greater than zero otherwise.
🌐
Scaler
scaler.com › home › topics › how to compare character in java
How to Compare Character in Java - Scaler Topics
May 12, 2024 - Using the Character class's charValue() method, we will extract character values from an object. A value that is greater than a, greater than b, or equal to both will be returned when we compare those values with relational operators.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_equals_char.htm
Java Arrays equals(char[], char[]) Method
The Java Arrays equals(char[] a, char[] a2) method returns true if the two specified arrays of chars are equal to one another. Two arrays are equal if they contain the same elements in the same order.
🌐
Delft Stack
delftstack.com › home › howto › java › java char equals in java
The Char equals Method in Java | Delft Stack
December 10, 2023 - This illustrates a simple and effective way to check character equality in Java using the == equal operator.
🌐
Quora
quora.com › How-do-you-compare-chars-in-Java
How to compare 'chars' in Java - Quora
Answer (1 of 7): The ‘char' primitive data type also has an associated integer value based on the Unicode table. Thus, you should be able to compare two characters as you would compare two integers using ==, operators. Additionally, you can store the chars in the Character wrapper class...
🌐
TestMu AI Community
community.testmuai.com › ask a question
How do I properly compare char values using equals() in Java? - Ask a Question - TestMu AI Community
March 24, 2025 - Since String is an object, we use string.equals(string) to check equality. However, when comparing char values, we use char1 == char2 instead of char1.equals(char2). Why is that? Does char equals in Java work differentl…
🌐
Java2Blog
java2blog.com › home › core java › string › character › how to compare characters in java
How to compare characters in Java - Java2Blog
January 11, 2021 - We can use relational operators like less than or greater than to compare two characters in Java. It is simplest approach and does not involve any class or method. ... You can compare primitive chars either using Character.compare() method or equals() method.
🌐
Coderanch
coderanch.com › t › 615778 › java › test-char-equals
How to test if a char equals something (Beginning Java forum at Coderanch)
July 14, 2013 - Bear Bibeault wrote: If you are trying to compare it against the character '1', then the expression would be a == '1'. This was the answer to my question.
🌐
LabEx
labex.io › tutorials › java-how-to-understand-the-difference-between-and-equals-for-character-objects-415249
How to understand the difference between == and equals() for Character objects | LabEx
By following these best practices, you can ensure that your code works correctly and efficiently when dealing with Character objects in Java. In this Java tutorial, you have learned the fundamental differences between using the == operator and the equals() method when comparing Character objects.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Character.html
Character (Java Platform SE 8 )
March 16, 2026 - Returns a hash code for this Character; equal to the result of invoking charValue().
🌐
Coderanch
coderanch.com › t › 597604 › java › charAt-equals-newbie
Need some help about .charAt and .equals just a newbie. (Beginning Java forum at Coderanch)
November 11, 2012 - Indicates whether some other object is "equal to" this one. So you use equals only when you want to compare two object references. char is primitive type, and you can't invoke equals method on variable of char type. Thus, you compare it as you would compare two variables of any other primitive ...
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.

🌐
Studytonight
studytonight.com › java-wrapper-class › java-character-equals-method
Java Character equals() Method - Studytonight
November 6, 2020 - Java equals() method belongs to the Character class. This method is used to compare the value of the Character object currently used with the value of the parameter.