c++ 中的字符串相等问题
#include
#include
using namespace std;
int main()
{
char strA[] = "aaa";
cout << "char stra[] = "aaa"" << endl;
cout << ""aaa" == strA : " << ("aaa" == strA) << endl; // 0
cout << "strcmp("aaa", strA) : " << strcmp("aaa", strA) << endl; // 0
const char * strP = "aaa";
cout << endl << "const char * strP = "aaa"" << endl;
cout << ""aaa" == strP : " << ("aaa" == strP) << endl; // 1
cout << "strcmp("aaa", strP) : " << strcmp("aaa", strP) << endl; // 0
cout << endl;
cout << "strA == strP : " << (strA == strP) << endl; // 0
cout << "strcmp(strA, strP) : " << (strcmp(strA, strP)) << endl; // 0
return 0;
}
java中的字符串相等问题
public class StrEqual {
public static void main(String[] args) {
String strA = "aaa";
System.out.println("strA = "aaa"");
System.out.println(""aaa" == strA : " + ("aaa" == strA)); // true
System.out.println("strA.equals.("aaa") : " + (strA.equals("aaa"))); //true
String strB = new String("aaa");
System.out.println("
" + "strB = new String("aaa")");
System.out.println(""aaa" == strB : " + ("aaa" == strB)); // false
System.out.println("strB.equals("aaa") : " + (strB.equals("aaa"))); //true
System.out.println();
System.out.println("strA.equals(strB) : " + (strA.equals(strB))); //true
System.out.println("strA == strB : " + (strA == strB)); // false
}
}