Maitiro Ekuenzanisa Tambo muJava

Kuti uenzanise Tambo dzekuenzana, iwe unofanirwa kushandisa iyo String chinhu equals kana equalsIgnoreCase nzira. Tichaonawo kuti sei tisingafanirwe kushandisa == opareta kuenzanisa tambo.

Kufananidza Tambo neyakaenzana () Maitiro

Kana isu tichifanirwa kuenzanisa tambo mbiri muJava uye zvakare hanya nezve kukamurwa kwetambo dzatinogona kushandisa equals() nzira.

Semuenzaniso, inotevera snippet ichaona kana zviitiko zviviri zveString zvakaenzana pane ese mavara kusanganisira 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');
}
} }

Kubuda:

first and second strings are equal Ongorora:Chirevo chechipiri chekudhinda hachiurayiwe nekuti iko kwekutanga kweString nechitatuString hazvienzane

Kuenzanisa Tambo neakaenzanaIgnoreCase () Maitiro

Kana isu tichifanirwa kuenzanisa tambo mbiri muJava asi tisina hanya nezve kukamurwa kwetambo dzatinogona kushandisa equalsIgnoreCase() nzira.

Semuenzaniso, mune iri pamusoro kodhi snippet, kana tikatsiva .equals() na .equalsIgnoreCase() nzira, ipapo zvese zvirevo zvokudhinda zvinoitwa:



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');
}
} }

Kubuda:

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

Inoenderana:

Usashandise == mushandisi kuenzanisa Tambo

Ongorora:Kana tichienzanisa tambo mbiri muJava, hatifanire kushandisa iyo == kana ! = vashandisi.

Ava vanoshanda vanonyatso kuyedza mareferenzi, uye sezvo zvinhu zvakawanda zveString zvinogona kumiririra iyo Tambo, izvi zvine mhosva yekupa mhinduro isiriyo.

Pane kudaro, shandisa String.equals(Object) nzira, iyo ichaenzanisa iyo String zvinhu zvichibva pamitengo yavo.

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');
}
} }

Kubuda:

first and second strings are equal Ongorora:Chirevo chechipiri chekudhinda hachiitwe.

Kufananidza Tambo neConstant Values

Paunenge uchifananidza Tambo nekukosheswa kukosha, iwe unogona kuisa iyo yenguva dzose kukosha kuruboshwe kuruboshwe zvakaenzana kuona kuti haugone kuwana NullPointerException kana imwe tambo isiri.

Semuyenzaniso:

'baz'.equals(foo)

Ipo foo.equals('baz') ndichakanda NullPointerException kana foo isingabatsiri, 'baz'.equals(foo) ichaongorora ku false.

Imwe nzira inoverengwa ndeyokushandisa Objects.equals(), Inoita cheki null pane ese maviri paramende:

semuenzaniso Objects.equals(foo, 'baz').

Kufananidza Tambo mune Shanduko Chirevo

Sezvo zve Java 1.7, zvinokwanisika kuenzanisa iyo String inoshanduka kune zvinyorwa mu switch switch. Ita shuwa kuti iyo Tambo haina kukona, zvikasadaro inogara ichikanda NullPointerException. Maitiro anofananidzwa uchishandisa String.equals, i.e. kesi inonzwisisika.

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;
}
} }

Mhedziso

Mune ino posvo takatsanangura maitiro ekuenzanisa tambo muJava nemienzaniso yekodhi. Kana kukaka tambo kuchikosha, isu tinofanirwa kushandisa .equals() uye kana kukanda kusiri kukosha, saka tinofanira kushandisa .equalsIgnoreCase().

Uyezve, isu hatifanire kushandisa iyo _ _ + _ | mushandisi kuenzanisa tambo, se == opareta anotarisa chirevo uye kwete kukosha.