logo

Java-tegn er lig med() Metode

Metoden equals(Object obj) i karakterklassen sammenligner objektet med det angivne objekt. Resultatet er sandt, hvis og kun hvis argumentet ikke er null, og Character-objektet har den samme char-værdi som repræsenteret af objektet.

Syntaks

 public boolean equals(Object obj) 

Parameter

Ovenstående metode kræver kun én parameter:

Objektet, der skal sammenlignes.

Returværdi

Metoden equals(Object obj) returnerer sand, hvis objekterne er ens. Ellers returnerer metoden falsk.

Eksempel 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.'); } } } 
Test det nu

Produktion:

 They are equal. 

Eksempel 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.'); } } } 
Test det nu

Produktion:

 They are not equal. 

Eksempel 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.'); } } } 
Test det nu

Produktion:

 The numbers are equal. 

Eksempel 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.'); } } } 
Test det nu

Produktion:

 The numbers are not equal.