final、finally、finalize的区别?
== 和 equals() 的区别?
:::success
一、回顾 == 的使用:
== :运算符
- 可以使用在基本数据类型变量和引用数据类型变量中
- 如果比较的是基本数据类型变量:比较两个变量保存的数据是否相等。(不一定类型要相同)
如果比较的是引用数据类型变量:比较两个对象的地址值是否相同,即两个引用 是否指向同一个对象实体
* 补充: == 符号使用时,必须保证符号左右两边的变量类型一致。
二、equals()方法的使用:
- 是一个方法,而非运算符
- 只能适用于引用数据类型
- Object类中equals()的定义:
public boolean equals(Object obj){return (this == obj)}
* 说明:Object类中定义的equals()和==的作用是相同的:比较两个对象的地址值是否相同.即两个引用是否指向同一个对象实体
- 像String、Date、File、包装类等都重写了Object类中的equals()方法。重写以后,比较的不是两个引用的地址是否相同,而是比较两个对象的”实体内容”是否相同。
- 通常情况下,我们自定义的类如果使用equals()的话,也通常是比较两个对象的”实体内容”是否相同。那么,我们就需要对Object类中的equals()进行重写.
*重写的原则:比较两个对象的实体内容是否相同。 :::
多态是编译时行为还是运行时行为?

package com.atguigu.test;import java.util.Random;//面试题:多态是编译时行为还是运行时行为?//证明如下:class Animal {protected void eat() {System.out.println("animal eat food");}}class Cat extends Animal {protected void eat() {System.out.println("cat eat fish");}}class Dog extends Animal {public void eat() {System.out.println("Dog eat bone");}}class Sheep extends Animal {public void eat() {System.out.println("Sheep eat grass");}}public class InterviewTest {public static Animal getInstance(int key) {switch (key) {case 0:return new Cat ();case 1:return new Dog ();default:return new Sheep ();}}public static void main(String[] args) {int key = new Random().nextInt(3);System.out.println(key);Animal animal = getInstance(key);animal.eat();}}
package com.atguigu.test;//考查多态的笔试题目:public class InterviewTest1 {public static void main(String[] args) {Base base = new Sub();base.add(1, 2, 3);// Sub s = (Sub)base;// s.add(1,2,3);}}class Base {public void add(int a, int... arr) {System.out.println("base");}}class Sub extends Base {public void add(int a, int[] arr) {System.out.println("sub_1");}// public void add(int a, int b, int c) {// System.out.println("sub_2");// }}
关于包装类使用的面试题


package com.atguigu.java2;import org.junit.Test;/** 关于包装类使用的面试题***/public class InterviewTest {@Testpublic void test1() {Object o1 = true ? new Integer(1) : new Double(2.0);System.out.println(o1);// 1.0}@Testpublic void test2() {Object o2;if (true)o2 = new Integer(1);elseo2 = new Double(2.0);System.out.println(o2);// 1}@Testpublic void test3() {Integer i = new Integer(1);Integer j = new Integer(1);System.out.println(i == j);//false//Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],//保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在//-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率Integer m = 1;Integer n = 1;System.out.println(m == n);//trueInteger x = 128;//相当于new了一个Integer对象Integer y = 128;//相当于new了一个Integer对象System.out.println(x == y);//false}}
