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 OverflowHello!
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!
Videos
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");
}
}
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.
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 (' ').
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.
Equalswouldn'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
charandStringare 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 ofStringandcharvalues.You should NEVER compare Strings or String literals using
==. The==operator on a reference type compares object identity, not object value. In the case ofStringit 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.