一. java中字符串比较注意事项:
1.==:比较双方的地址,而不是比较内容
2 .比较到:比较双方的内容,而不是比较地址
3. equals:比较字符串对象。 这是因为equals的真实身份如下。
布尔表达式(对象;
4 .常量字符串比较。
二.例子:
公共类字符串比较{
publicstaticvoidmain (string args [ ] ) { String s='a '; 字符串t=' a '; 字符串x=' b '; 系统. out.println (s.equals (t ) ); //true system.out.println (s.com Pareto (t ) ); //0system.out.println(s==t ); //true,system.out.println(s.equals(x ) ); //false system.out.println (s.com Pareto (x ) ); //-1system.out.println(s==x ); //false,stringstr=newstring('s ); stringstr2=newstring('s ); system.out.println(str.equals ) str2); //true system.out.println (str.com Pareto ) str2); //0 system.out.println (str==str2); //false } } /* * .注意:常量字符串和new中新对象的区别在于str==str2 */