Java面试之2018

tips :之前9月份离职,休息一段时间后开始面试,正好赶上互联网寒冬时期,面试准备又不够充分,导致目前还在继续面试,稍微惨,总结了近期面试的一些题目,可以给大家一些参考,大神可忽略,话不多说,上货!(有错还请指正)


一、Java基础

1.equals和==的区别

类型equals==
String Integter Charcater double float  short boolean
普通类(重写equals)内容地址
普通类(未重写equals)地址地址

 

普通两个类要相等要重写equals和hashCode方法

notes:Spring框架没有标明情况下,创建的bean都是单例的,那两个请求中bean里的参数值变了,对象仍是相等的吗?

2.String StringBulider StringBuffer

StringBuffer是线程安全,因为源码在实现的时候使用了Synchronized关键字

StringBulider非线程安全

3.单例模式 懒汉 饿汉  破坏一个单例模式的方法及如何优化

/**
 * 饿汉模式
 */
public class HungryExam {

    public HungryExam() {
    }

    private static  HungryExam instance = new HungryExam();

    public static HungryExam getInstance(){

        return instance;
    }
}
/**
 * 懒汉模式
 */
public class LazyExam {

    public LazyExam() {
    }

    private  volatile static LazyExam instance ;

    public static LazyExam getInstance(){
        if(instance == null){
            synchronized (LazyExam.class){
                if(instance == null){
                    instance = new LazyExam();
                }
            }
        }
        return instance;
    }
破坏方法:

public static void main(String[] args) {
    //正常创建
    for (int i = 0 ; i < 10; i++){
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                LazyExam lazyExam = LazyExam.getInstance();
                System.out.println("获取到的单例对象:"+ lazyExam);
            }
        });
        t.start();
    }
    //反射
    Class c = Class.forName("test.string2int.LazyExam");
    Object o = c.newInstance();
    if(o instanceof LazyExam){
        System.out.println("获取到的单例对象-反射:"+ o);
    }

}
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象-反射:test.string2int.LazyExam@4554617c
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd
获取到的单例对象:test.string2int.LazyExam@252708dd

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值