zoukankan      html  css  js  c++  java
  • java 基础知识面试题

    https://blog.csdn.net/yeyazhishang/article/details/102886489

    https://www.cnblogs.com/zwwhnly/p/10826530.html

    1. == 和 equals 区别?

      == : 判断是否指向同一内存地址,Object的equals 就是用 == 实现的,如果想判断两个对象是否

     相等,可以覆写 equals 方法,比如String、Data

            String strA = new String("hello");
            String strB = new String("hello");
    
            System.out.println(strA ==strB); //false
            System.out.println(strA.equals(strB)); //true

    2. 覆写equals 时,为什么要覆写hashCode 方法?

      如果不覆写hashCode方法,比如 HashMap 等容器,首先会使用hash来判断,如果两个对象相等,则hash 默认为是相等的,

       HashMap中部分源码:

      Node<K,V> e; K k;
                if (p.hash == hash &&
                    ((k = p.key) == key || (key != null && key.equals(k))))
                    e = p;

      所以为了使用容器不出错,最好覆写hashCode 方法。

    3. List 和 Set 区别?

        List :允许重复的元素、有序的;

        Set: 不允许重复的元素、无序的;

        ArrayList的初始容量是10,加载因子为0.5; 

        HashMap、HashSet,初始容量16,加载因子为0.75;

    4. java String, StringBuffer ,StringBulider 区别  

        1)String 是不可变的(immutable); 创建后不能修改;

        2)StringBuffer 线程安全的,可变字符串;

        3)StringBuilder 线程不安全,所以性能比较高

    5. static 应用场景? 

       1)只想用一个存储区域来保存一个特定的数据,不需要创建对象;
       2)没有创建对象,也能调用方法;

    6. 到底选择合成还是继承?   

      一个最简单的办法就是考虑是否需要从新类上溯造型回基础类。若必须上溯,就
       需要继承。但如果不需要上溯造型,就应提醒自己防止继承的滥用。

       上溯造型(upcasting):

    //: Wind.java
    // Inheritance & upcasting
    import java.util.*;
    class Instrument {
    public void play() {}
    static void tune(Instrument i) {
    // ...
    i.play();
    }
    }
    // Wind objects are instruments
    // because they have the same interface:
    class Wind extends Instrument {
    public static void main(String[] args) {
    Wind flute = new Wind();
    Instrument.tune(flute); // Upcasting

    7. protected

        1) 子类和同一包内的其他可以访问

        在实际应用中,经常想把某些东西深深地藏起来,但同时允许访问衍生类的成员。

       

    //: Orc.java
    // The protected keyword
    import java.util.*;
    class Villain {
    private int i;
    protected int read() { return i; }
    protected void set(int ii) { i = ii; }
    public Villain(int ii) { i = ii; }
    public int value(int m) { return m*i; }
    }
    public class Orc extends Villain {
    private int j;
    public Orc(int jj) { super(jj); j = jj; }
    public void change(int x) { set(x); }
    } ///:~
  • 相关阅读:
    CF 118E Bertown roads 桥
    hdu 3917 Road constructions 最大权闭合子图
    hdu 4714 Tree2cycle 树形经典问题
    POJ 2516 Minimum Cost 最小费用流
    POJ 3921 Destroying the bus stations 沿着最短路迭代加深搜索
    POJ 3422 Kaka's Matrix Travels K取方格数
    BZOJ 3083: 遥远的国度 dfs序,树链剖分,倍增
    hdu 4010 Query on The Trees LCT
    poj 2455 Secret Milking Machine 二分+最大流 sap
    定制标记---简单标记处理器
  • 原文地址:https://www.cnblogs.com/newlangwen/p/11822811.html
Copyright © 2011-2022 走看看