java 字符串String比较要注意

来源:互联网 发布:审稿人怀疑数据真实性 编辑:程序博客网 时间:2024/04/29 08:03
public class Test {public static void main(String[] args) {String s1="100";String s2=new String("100");//字符串比较尽量不要用s1==s2这种方式,这种必须相同对象的字符串,而s1和s2是不同对象的if(s1==s2){System.out.println("==");}else{System.out.println("!=");}//输出!=if(s1.compareTo(s2)==0){System.out.println("==");}else{System.out.println("!=");}//输出==if(s1.equals(s2)){System.out.println("==");}else{System.out.println("!=");}//输出==}

0 0
原创粉丝点击