JavaSE基础试题

2020Java面试题及答案(个人理解,仅供参考)

1. 面向对象的三大特征及特点

  • 封装:封装就是隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别,将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。
  • 继承:继承是面向对象的基本特征之一,继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。
  • 多态:多态同一个行为具有多个不同表现形式或形态的能力。实现多态有三个必要条件:重写,继承,父类引用指向子类。多态简单来说就是不同子类对同一个对象做出不同的响应。

2. JRE与JDK有什么区别

  • JDK:简称Java开发工具包,提供了Java开发的环境和运行环境。
  • JRE:简称Java开发环境,提供了Java运行的环境,有这个就能运行Java文件

3. == 和equals有什么区别

  • ==对基本数据类型是值的比较,对引用类型比较的是引用
  • equals比较的是引用是否相等
		//String重写了equals方法
	    String a =new String("a");
		String b =new String("a");
		String c="c";
		String d="c";
		String e="a";
		System.out.println(a==b);//false
		System.out.println(a.equals(b)); //true
		System.out.println(c==d);//true
		System.out.println(c.equals(d)); //true
		System.out.println(a==e); //false
		System.out.println(a.equals(e)); //true

4. String是不是基本数据类型?

  • String不是基本数据类型,String 是java.lang 底下的(记住String 是final的 所以不能继承,但提供了许多静态方法)。
  • 基本的数据类型有:byte,char, short,int,folat,double,long,boolean

5. final在Java中有什么作用

  • final修饰的类不能被继承
  • final修饰的方法不能被重写
  • final修饰的变量叫常量,常量的值初始化后不可改变

6.int和Integer的区别
Java提供了引用类型和基本数据类型,int属于基本数据类型默认值为0
Integer属于引用类型,默认值为null
Int没有方法,Integer提供很多方法

7.String str="i"与 String str=new String(“i”)一样吗?

  • 不一样,内存分配不一样。String str="i"分配在常量池中,String str=new String(“i”)分配在堆内存中。

8.String 和StringBuffer,StringBuilder的区别

  • String:String 声明的是不可变的对象,每次操作都会生成新的 String 对象,然后将指针指向新的 String 对象,而 StringBuffer、StringBuilder 可以在原有对象的基础上进行操作,所以在经常改变字符串内容的情况下最好不要使用 String。
  • StringBuffer 和 StringBuilder 最大的区别在于,StringBuffer 是线程安全的,而 StringBuilder 是非线程安全的,但 StringBuilder 的性能却高于 StringBuffer,所以在单线程环境下推荐使用 StringBuilder,多线程环境下推荐使用 StringBuffer。

9.说出Servlet的生命周期
Servlet被服务器实例化后,容器运行init方法,请求到service方法,service
方法自动根据请求的方式调用相应的doget或者dopost方法当服务器决定销毁对象的时候调用destroy方法

10.List,set,Map的区别

比较 List Set Map
继承的接口 Collection Conllection
常见的实现类 AbstractList(常见的子类有:ArrayList,LinkedList,Vector) AbstractSet (常见子类 hashSet,TreeSet,LinkedhashSet) Map(常见子类:hashMap,hashtable)
元素 可重复 不可重复 不可重复
排序 有序 无序
线程安全 Vector hashTable

11.Collection 和 Collections的区别。

  • Collection 是List跟Set的集合类的上级接口
  • Collections是针对集合类的一个帮助类,他提供一系列静态方法实现对各种集合的搜索、排序、线程安全化等操作。

12. 两个对象的 hashCode()相同,则 equals()也一定为 true吗

  • 两个对象hashCode相同 equals不一定为true;
		String aa="柳柴";
		String bb="柴柕";
		System.out.println(String.format("aa:%d | bb:%d",  aa.hashCode(),bb.hashCode()));
		System.out.println(aa.equals(bb));

输出:aa:851553 | bb:851553
false
这说明了hashcode 相同 equals不一样为true;

13. 怎么计算2乘8的速度最快

  • 2<<3

14. java 中的 Math.round(-2.5) 等于多少?

  • 等于 -2,因为在数轴上取值时,中间值(0.5)向右取整,所以正 0.5 是往上取整,负 0.5 是直接舍弃。

15.String 类的常用方法都有那些?

  • indexOf():返回指定字符的索引。
  • charAt():返回指定索引处的字符。
  • replace():字符串替换。
  • trim():去除字符串两端空白。
  • split():分割字符串,返回一个分割后的字符串数组。
  • getBytes():返回字符串的 byte 类型数组。
  • length():返回字符串长度。
  • toLowerCase():将字符串转成小写字母。
  • toUpperCase():将字符串转成大写字符。
  • substring():截取字符串。
  • equals():字符串比较。

16.普通类和抽象类有哪些区别??

  • 普通类不能包含抽象方法,抽象类可以包含普通方法
  • 普通类可以直接实例化,抽象类不能直接实例化

17.说说抽象类能使用 final 修饰吗?

  • 抽象类不能用final修饰,因为抽象类需要被继承,而final修饰的类不能被继承。
                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like  tool-clicked"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count">1</span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xiaoyanghnl">
                <img src="https://profile.csdnimg.cn/E/9/C/3_xiaoyanghnl" class="avatar_pic" username="xiaoyanghnl">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/2x/1.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xiaoyanghnl" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">Mr.JiaWan</a></span>
                                        </div>
                <div class="text"><span>发布了3 篇原创文章</span> · <span>获赞 1</span> · <span>访问量 240</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xiaoyanghnl" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
发布了1 篇原创文章 · 获赞 0 · 访问量 10

2020Java面试题及答案(个人理解,仅供参考)

1. 面向对象的三大特征及特点

  • 封装:封装就是隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别,将抽象得到的数据和行为(或功能)相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成“类”,其中数据和函数都是类的成员。
  • 继承:继承是面向对象的基本特征之一,继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。
  • 多态:多态同一个行为具有多个不同表现形式或形态的能力。实现多态有三个必要条件:重写,继承,父类引用指向子类。多态简单来说就是不同子类对同一个对象做出不同的响应。

2. JRE与JDK有什么区别

  • JDK:简称Java开发工具包,提供了Java开发的环境和运行环境。
  • JRE:简称Java开发环境,提供了Java运行的环境,有这个就能运行Java文件

3. == 和equals有什么区别

  • ==对基本数据类型是值的比较,对引用类型比较的是引用
  • equals比较的是引用是否相等
		//String重写了equals方法
	    String a =new String("a");
		String b =new String("a");
		String c="c";
		String d="c";
		String e="a";
		System.out.println(a==b);//false
		System.out.println(a.equals(b)); //true
		System.out.println(c==d);//true
		System.out.println(c.equals(d)); //true
		System.out.println(a==e); //false
		System.out.println(a.equals(e)); //true

猜你喜欢

转载自blog.csdn.net/qq_45452081/article/details/104917733