Metoda equals(Object obj) razreda znakov primerja objekt z navedenim objektom. Rezultat je resničen, če in samo če argument ni nič in ima objekt Character enako vrednost char, kot jo predstavlja objekt.
Sintaksa
public boolean equals(Object obj)
Parameter
Zgornja metoda zahteva samo en parameter:
Predmet, ki ga je treba primerjati.
Povratna vrednost
Metoda equals(Object obj) vrne true, če so objekti enaki. V nasprotnem primeru metoda vrne false.
Primer 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.'); } } }Preizkusite zdaj
Izhod:
They are equal.
Primer 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.'); } } }Preizkusite zdaj
Izhod:
They are not equal.
Primer 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.'); } } }Preizkusite zdaj
Izhod:
The numbers are equal.
Primer 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.'); } } }Preizkusite zdaj
Izhod:
The numbers are not equal.