logo

Java Character equals() Metod

Metoden equals(Object obj) i teckenklassen jämför objektet med det angivna objektet. Resultatet är sant om och endast om argumentet inte är null och Character-objektet har samma char-värde som representeras av objektet.

Syntax

 public boolean equals(Object obj) 

Parameter

Metoden ovan kräver bara en parameter:

Objektet som behöver jämföras.

Returvärde

Metoden equals(Object obj) returnerar sant om objekten är samma. Annars returnerar metoden false.

Exempel 1

 public class JavaCharcterEqualsExample1 { public static void main(String[] args) { //Create a Character Character charValue = new Character('A'); // Create an Object Object obj = new Character('A'); // Test the equality for two values if (charValue.equals(obj)) { System.err.println('They are equal.'); } else { System.err.println('They are not equal.'); } } } 
Testa det nu

Produktion:

 They are equal. 

Exempel 2

 public class JavaCharacterEqualsExample2 { public static void main(String[] args) { //Create a Character Character charValue = new Character('B'); // Create an Object Object obj1 = new Character('C'); // Test the equality for two values if (charValue.equals(obj1)) { System.err.println('They are equal.'); } else { System.err.println('They are not equal.'); } } } 
Testa det nu

Produktion:

 They are not equal. 

Exempel 3

 public class JavaCharacterEqualsExample3 { public static void main(String[] args) { //Create a Character Character charValue = new Character('1'); // Create an Object Object obj = new Character('1'); // Test the equality for two values if (charValue.equals(obj)) { System.err.println('The numbers are equal.'); } else { System.err.println('The numbers are not equal.'); } } } 
Testa det nu

Produktion:

 The numbers are equal. 

Exempel 4

 public class JavaCharacterEqualsExample4 { public static void main(String[] args) { //Create a Character Character charValue = new Character('2'); // Create an Object Object obj = new Character('3'); // Test the equality for two values if (charValue.equals(obj)) { System.err.println('The numbers are equal.'); } else { System.err.println('The numbers are not equal.'); } } } 
Testa det nu

Produktion:

 The numbers are not equal.