zoukankan      html  css  js  c++  java
  • equals与==的区分

    equals与==的区分

    对于比较数值

    public class Test {
    public static void main(String[] args){
    	int a=30;
    	int b=30;
    	System.out.println("hellow world!");
    	System.out.println(a==b);
    	}
    }
    
    运行结果:
    hellow world!
    true
    

    对于比较字符串时,

    ==比较的是地址,其内容分别保存在了不同的空间,所以即使内容相等,但是地址的值是不相等的。

    public class Test {
    public static void main(String[] args){
    	String a="hellow";
    	String b=new String("hellow");
    	String c=b;
    	//System.out.println("hellow world!");
    	System.out.println(a==b);
    	System.out.println(a==c);
    	System.out.println(b==c);
    	}
    }
    运行结果:
    false
    false
    true
    

    而equals只是比较的是字符串内容而不是地址,但是这里涉及到数据库char和varcha的区别,空格equals是能识别出来的。

    public class Test {
    public static void main(String[] args){
    	String a="hellow";
    	String b=new String("hellow");
    	String c=b;
    	//System.out.println("hellow world!");
    	System.out.println(a.equals(b));
    	System.out.println(a.equals(c));
    	System.out.println(b.equals(c));
    	}
    }
    运行结果:
    true
    true	
    true
    
    • 更多精彩内容,请关注微信关注公众号 明叶师兄的学堂
  • 相关阅读:
    利用docker搭建测试环境--安装
    fiddler获取手机请求
    python多线程
    linux下安装python的第三方module
    shell编程sed笔记
    shell 函数
    mysql information_schema 数据库简介:
    shell常用的判断条件
    gulp:gulp-sass基础教程
    (六):关于全局config配置
  • 原文地址:https://www.cnblogs.com/renxiuxing/p/8537379.html
Copyright © 2011-2022 走看看