您的位置:首页 > 职场人生

java基础面试题总结

2018-03-27 14:33 239 查看
1.java中==和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方法进行了重写的话,比较的是所指向的对象的内容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: