java中"==" 与equals方法的使用_Java教程-查字典教程网
java中"==" 与equals方法的使用
java中"==" 与equals方法的使用
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:publicclassequalsDemo{publicstaticvoidmain(String[]args){/*使用...

复制代码 代码如下:

public class equalsDemo {

public static void main(String[] args){

/*使用==来判断两个变量是否相等时,如果两个变量时基本数据类型的

变量时,且都是数值类型是,则只要两个变量的值相等,使用==判断就返回true*/

int i=65;

float f=65.0f;

System.out.println(i==f);//true

char c='A';

System.out.println(c==f);//true

//但是对于两个引用类型的变量,必须它们指向同一个对象时,==判断才会返回true

String str1=new String("hello");

String str2=new String("hello");

System.out.println(str1==str2);//false

System.out.println(str1.equals(str2));//true

}

}

复制代码 代码如下:

public class IntegerDemo{

public static void main(String[] args){

Integer i1 =127;

Integer i2 =127;

System.out.println(i1==i2);//true

Integer i3 =128;

Integer i4 =128;

System.out.println(i3==i4);//false

//享元模式

}

}

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Java学习
    热门Java学习
    编程开发子分类