logo

La stringa Java è uguale a()

IL La classe String Java è uguale a() Il metodo confronta le due stringhe fornite in base al contenuto della stringa. Se un carattere non corrisponde, restituisce false. Se tutti i caratteri corrispondono, restituisce true.

Il metodo String equals() sovrascrive il metodo equals() della classe Object.

Firma

 publicboolean equals(Object anotherObject) 

Parametro

un altroOggetto : un altro oggetto, cioè rispetto a questa stringa.

unix crea la directory

ritorna

VERO se i caratteri di entrambe le stringhe sono uguali altrimenti falso .

Implementazione interna

 public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String) anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; } 

Esempio del metodo Java String equals()

Nome del file: Uguale ad Esempio.java

 public class EqualsExample{ public static void main(String args[]){ String s1='javatpoint'; String s2='javatpoint'; String s3='JAVATPOINT'; String s4='python'; System.out.println(s1.equals(s2));//true because content and case is same System.out.println(s1.equals(s3));//false because case is not same System.out.println(s1.equals(s4));//false because content is not same }} 
Provalo adesso

Produzione:

 true false false 

Metodo Java String equals() Esempio 2

Il metodo equals() confronta due stringhe e può essere utilizzato nella struttura di controllo if-else.

Nome del file: Uguale a Esempio2.java

 public class EqualsExample2 { public static void main(String[] args) { String s1 = 'javatpoint'; String s2 = 'javatpoint'; String s3 = 'Javatpoint'; System.out.println(s1.equals(s2)); // True because content is same if (s1.equals(s3)) { System.out.println('both strings are equal'); }else System.out.println('both strings are unequal'); } } 

Produzione:

 true both strings are unequal 

Metodo Java String equals() Esempio 3

Vediamo un altro esempio per testare l'uguaglianza delle stringhe presenti nella lista.

converti int in double java

Nome del file: Uguale a Esempio3.java

'algoritmo di Kruskal'
 import java.util.ArrayList; public class EqualsExample3 { public static void main(String[] args) { String str1 = 'Mukesh'; ArrayList list = new ArrayList(); list.add('Ravi'); list.add('Mukesh'); list.add('Ramesh'); list.add('Ajay'); for (String str : list) { if (str.equals(str1)) { System.out.println('Mukesh is present'); } } } } 

Produzione:

 Mukesh is present 

Metodo Java String equals() Esempio 4

L'implementazione interna del metodo equals() mostra che è possibile passare il riferimento di qualsiasi oggetto nel parametro del metodo. L'esempio seguente mostra lo stesso.

Nome del file: Uguale a Esempio4.java

 public class EqualsExample4 { // main method public static void main(String argvs[]) { // Strings String str = 'a'; String str1 = '123'; String str2 = '45.89'; String str3 = 'false'; Character c = new Character('a'); Integer i = new Integer(123); Float f = new Float(45.89); Boolean b = new Boolean(false); // reference of the Character object is passed System.out.println(str.equals(c)); // reference of the Integer object is passed System.out.println(str1.equals(i)); // reference of the Float object is passed System.out.println(str2.equals(f)); // reference of the Boolean object is passed System.out.println(str3.equals(b)); // the above print statements show a false value because // we are comparing a String with different data types // To achieve the true value, we have to convert // the different data types into the string using the toString() method System.out.println(str.equals(c.toString())); System.out.println(str1.equals(i.toString())); System.out.println(str2.equals(f.toString())); System.out.println(str3.equals(b.toString())); } } 

Produzione:

 false false false false true true true true