Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How do I use the character's equals() method in Java?

Writer Emily Wong

As the String is an object, you need to use the string.equals(string) method to confirm that the two strings are equal.

However, why do you use the == to check if two chars are equal, char1 == char2, rather than char1.equals(char2)?

3

5 Answers

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 JavaOr over here:

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.

Basically Java has primitive types (int, char, short, long, byte ....) and Reference Data types/ Objects composed of other primitives and Objects.

equals() is a method of all Java Objects. But char is not an Object type in Java, it is a primitive type, it does not have any method or properties, so to check equality they can just use the == equals operator.

You can also use a comparator if you want.

 public static boolean compareChars(char c1, char c2){ int comp = Character.compare(c1, c2); if(comp>0){ return false; }else{ return true; } } public static void main (){ boolean b1 = compareChars('A', 'A') //return true; boolean b2 = compareChars('A', 'C') //return false; }

Because char is a primitive type and does not implement equals, == compares char values directly in this case, where as String is an object. So for object comparison, the equality operator is applied to the references to the objects, not the objects they point to. Two references are equal if and only if they point to the same object, or both point to null.

If you want to compare strings (to see if they contain the same characters), you need to compare the strings using equals().

Hence, the operator == checks equality of values on primitive types, but it checks references equality for objects. If the two objects are referenced by equal references, a reference and an object are different in Java.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy