Uyithelekisa njani imitya kwiJava

Ukuze uthelekise imitya yokulingana, kuya kufuneka usebenzise iiString object equals okanye equalsIgnoreCase iindlela. Siza kubona ukuba kutheni kungafuneki sisebenzise == umsebenzisi ukuthelekisa imitya.

Thelekisa imitya nee-equals () Indlela

Ukuba sifuna ukuthelekisa imitya emibini kwiJava kwaye sikhathalele nangokubamba imitya esinokuyisebenzisa equals() indlela.

Umzekelo, le snippet ilandelayo iya kugqiba ukuba ngaba ezi zimbini zeString ziyalingana kubo bonke abalinganiswa kubandakanya i-casing:

public class CompareTwoStrings {
public static void main(String[] args) {
String firstString = 'Test123';
String secondString = 'Test' + 123;
String thirdString = 'TEST123';

if (firstString.equals(secondString)) {

System.out.println('first and second strings are equal');
}

if (firstString.equals(thirdString)) {

System.out.println('first and third string are equal');
}
} }

Iziphumo:

first and second strings are equal Phawula:Isiteyitimenti sesibini sokuprinta asiqhutywa kuba ukubekwa kweString yokuqala kunye neyesithathuString azihambelani.

Thelekisa imitya ngeendlela ezilinganayoIgnoreCase ()

Ukuba sifuna ukuthelekisa imitya emibini kwiJava kodwa ungakhathali malunga nokufakwa kwemitya esinokuyisebenzisa equalsIgnoreCase() indlela.

Umzekelo, kule khowudi ingentla, ukuba sithathe indawo .equals() kunye .equalsIgnoreCase() indlela, emva koko zombini iinkcazo zokuprinta ziyenziwa:

public class CompareTwoStrings {
public static void main(String[] args) {
String firstString = 'Test123';
String secondString = 'Test' + 123;
String thirdString = 'TEST123';

if (firstString.equalsIgnoreCase(secondString)) {

System.out.println('first and second strings are equal');
}

if (firstString.equalsIgnoreCase(thirdString)) {

System.out.println('first and third string are equal');
}
} }

Iziphumo:

first and second strings are equal
first and third string are equal

Idibeneyo:

Sukusebenzisa umqhubi == ukuthelekisa imitya

Phawula:Xa uthelekisa imitya emibini kwiJava, akufuneki sisebenzise == okanye ! = abaqhubi.

Abaqhubi ngokwenyani bavavanya izingqinisiso, kwaye kuba izinto ezininzi eziSentambo zingabonisa umtya ofanayo, oku kufanelekile ukunika impendulo engeyiyo.

Endaweni yoko, sebenzisa String.equals(Object) indlela, eya kuthi ithelekise izinto zomtya ngokusekwe kumaxabiso abo.

public class CompareTwoStrings {
public static void main(String[] args) {
String firstString = 'Test123';
String secondString = 'Test123';
String thirdString = new String('Test123');

if (firstString == secondString) {

System.out.println('first and second strings are equal');
}

if (firstString == thirdString) {

System.out.println('first and third strings are equal');
}
} }

Iziphumo:

first and second strings are equal Phawula:Isiteyitimenti sesibini sokuprinta asiqhutywa.

Thelekisa imitya namaxabiso ahlala ehleli

Xa uthelekisa umtya kwixabiso elihlala lihleli, ungabeka ixabiso elingagungqiyo kwicala lasekhohlo lokulingana ukuqinisekisa ukuba awuyi kufumana NullPointerException ukuba omnye umtya awunto.

Umzekelo:

'baz'.equals(foo)

Ngelixa foo.equals('baz') uya kuphosa NullPointerException ukuba i-foo ayisebenzi, 'baz'.equals(foo) iya kuvavanya | | + _ |.

Olunye uhlobo olufundekayo kukusebenzisa false, eyenza itshekhi elilize kuzo zombini ezi paramitha:

umz. Objects.equals().

Thelekisa imitya kwiNgxelo yokuTshintsha

Ukusukela ngeJava 1.7, kunokwenzeka ukuthelekisa umahluko woMtya kunye noncwadi kwisitatimende sokutshintsha. Qiniseka ukuba umtya awusebenzi, kungenjalo iya kuhlala iphosa Objects.equals(foo, 'baz'). Amaxabiso athelekiswa kusetyenziswa NullPointerException, okt imeko yecala.

String.equals

Isiphelo

Kule posi sichaze indlela yokuthelekisa imitya kwiJava kunye nemizekelo yekhowudi. Xa kuthwalwa umtya, kufuneka sisebenzise public class CompareTwoStrings {
public static void main(String[] args) {
String stringToSwitch = 'A';


switch (stringToSwitch) {

case 'a':


System.out.println('a');


break;




case 'A':


System.out.println('A'); //the code goes here


break;




case 'B':


System.out.println('B');


break;




default:


break;
}
} }
kwaye xa ukugcinwa kungabalulekanga, kuya kufuneka sisebenzise .equals().

Ngaphezu koko, akufuneki sisebenzise .equalsIgnoreCase() umqhubi ukuthelekisa imitya, njenge == Umsebenzi ujonga ireferensi hayi ixabiso.