java基础面试题总结
2018-03-27 14:33
239 查看
1.java中==和equals
“==”的介绍:.基本数据类型,也称原始数据类型
byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==),比较的是他们的值
引用类型(类、接口、数组)
当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false。对象是放在堆中的,栈中存放的是对象的引用(地址)。由此可见’==’是对栈中的值进行比较的。如果要比较堆中对象的内容是否相同,那么就要重写equals方法了.
}
Integer b1 = 127;java在编译的时候,被翻译成-> Integer b1 = Integer.valueOf(127);注解:
Integer b1 = 127;java在编译的时候,被翻译成-> Integer b1 = Integer.valueOf(127);
看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer b1 = 127时,会将127进行缓存,下次再写Integer i6 = 127时,就会直接从缓存中取,就不会new了。所以a1==b1:true a==b:false
1.equals
可以看出,String类对equals方法进行了重写,用来比较指向的字符串对象所存储的字符串是否相等。
其他的一些类诸如Double,Date,Integer等,都对equals方法进行了重写用来比较指向的对象所存储的内容是否相等。
总结来说:
1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等;
如果作用于引用类型的变量,则比较的是所指向的对象的地址
2)对于equals方法,注意:equals方法不能作用于基本数据类型的变量
如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如String、Date等类对equals方法进行了重写的话,比较的是所指向的对象的内容。
“==”的介绍:.基本数据类型,也称原始数据类型
byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==),比较的是他们的值
引用类型(类、接口、数组)
当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false。对象是放在堆中的,栈中存放的是对象的引用(地址)。由此可见’==’是对栈中的值进行比较的。如果要比较堆中对象的内容是否相同,那么就要重写equals方法了.
public static void main(String[] args) { int int1 = 12; int int2 = 12; Integer Integer1 = new Integer(12); Integer Integer2 = new Integer(12); Integer Integer3 = new Integer(127); Integer a1 = 127; Integer b1 = 127; Integer a = 128; Integer b = 128; String s1 = "str"; String s2 = "str"; String str1 = new String("str"); String str2 = new String("str"); System.out.println("int1==int2:" + (int1 == int2)); //true System.out.println("int1==Integer1:" + (int1 == Integer1)); //int1==Integer1:true //Integer会自动拆箱为int,所以为true System.out.println("Integer1==Integer2:" + (Integer1 == Integer2)); //不同对象,在内存存放地址不同,所以为false System.out.println("Integer3==b1:" + (Integer3 == b1)); //Integer3指向new的对象地址,b1指向缓存中127地址,地址不同,所以为false System.out.println("a1==b1:" + (a1 == b1)); //true System.out.println("a==b:" + (a == b)); //false System.out.println("s1==s2:" + (s1 == s2)); //true System.out.println("s1==str1:" + (s1 == str1)); //false System.out.println("str1==str2:" + (str1 == str2)); //false
}
Integer b1 = 127;java在编译的时候,被翻译成-> Integer b1 = Integer.valueOf(127);注解:
Integer b1 = 127;java在编译的时候,被翻译成-> Integer b1 = Integer.valueOf(127);
public static Integer valueOf(int i) { assert IntegerCache.high >= 127; if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer b1 = 127时,会将127进行缓存,下次再写Integer i6 = 127时,就会直接从缓存中取,就不会new了。所以a1==b1:true a==b:false
1.equals
1、默认情况(没有覆盖equals方法)下equals方法都是调用Object类的equals方法,而Object的equals方法主要用于判断对象的内存地址引用是不是同一个地址(是不是同一个对象)。下面是Object类中equals方法: public boolean equals(Object obj) { return (this == obj); } 定义的equals与==是等效的 2,要是类中覆盖了equals方法,那么就要根据具体的代码来确定equals方法的作用了,覆盖后一般都是通过对象的内容是否相等来判断对象是否相等。下面是String类对equals进行了重写: public boolean equals(Object anObject) { if (this == anObject) { return true 4000 ; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = count; if (n == anotherString.count) { char v1[] = value; char v2[] = anotherString.value; int i = offset; int j = anotherString.offset; while (n-- != 0) { if (v1[i++] != v2[j++]) return false; } return true; } } return false; } 即String中equals方法判断相等的步骤是: 1.若A==B 即是同一个String对象 返回true 2.若对比对象是String类型则继续,否则返回false 3.判断A、B长度是否一样,不一样的话返回false 4。逐个字符比较,若有不相等字符,返回false
可以看出,String类对equals方法进行了重写,用来比较指向的字符串对象所存储的字符串是否相等。
其他的一些类诸如Double,Date,Integer等,都对equals方法进行了重写用来比较指向的对象所存储的内容是否相等。
总结来说:
1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等;
如果作用于引用类型的变量,则比较的是所指向的对象的地址
2)对于equals方法,注意:equals方法不能作用于基本数据类型的变量
如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如String、Date等类对equals方法进行了重写的话,比较的是所指向的对象的内容。
相关文章推荐
- Java基础学习总结(73)——Java最新面试题汇总
- 黑马程序员——Java基础---线程的另一个总结(10)--练习,空中网面试题总结
- Java面试题和基础总结(持续更新中…
- Java基础面试题总结(二)
- java基础面试题总结之一
- Java基础学习总结(73)——Java最新面试题汇总
- 今天看了一下,java的基础面试题,关于抽象类和抽象方法的总结
- Java基础面试题总结
- Java基础学习总结(108)——Java常见面试题再总结
- java基础面试题总结
- java基础知识精华总结 java面试题 深圳酷琪 kuuqii.com 电脑配件软件 硬件一体化采购平台
- Java基础面试题总结
- JAVA---基础面试题总结(1)
- Java基础面试题总结(一)
- Java基础面试题总结
- Day01-Java基础面试题总结(答案见Day02)
- Java基础学习总结(68)——有关Java线程方面的面试题
- [置顶] 超详细的Java面试题总结(一)之Java基础知识篇
- Java基础学习总结(68)——有关Java线程方面的面试题
- Java基础学习总结(104)——多线程、并发、工具类相关的面试题