Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, "pointer" equality and string comparison

I've C code that use a lot of commands identified by static string constants.

static char* KCmdA = "commandA"
static char* KCmdB = "commandB"
static char* KCmdC = "commandC"

In C I can compare two strings with strcmp(A, B) for example but as I only refer those command via their static string identifier it's faster to only check for pointer inequality since I know my unknowCMD can only be a pointer to one of my static strings.

switch(unknowCMD)
{
    case KCmdA:
    ...
    case KCmdB:
    ...
}

I guess in Java the equivalent to strcmp would be the method equals:

unknowCMD.equals(KCmdA)

Is there an equivalent of the pointer equality in Java ? I know Java uses only references. Is it possible to use those references for equality test without actually comparing the strings ?

Sorry if this is obvious I've check the doc but did not find any definitive answer.

like image 277
CodeFlakes Avatar asked Dec 13 '22 00:12

CodeFlakes


1 Answers

if you compare equality of string refrences Use ==

if(str1==str2){

}
like image 112
Samir Mangroliya Avatar answered Dec 31 '22 07:12

Samir Mangroliya