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="{"mod":"popu_824"}"><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="{"mod":"1582594662_002"}"><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="{"mod":"popu_379"}" 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="{"mod":"popu_379"}">關注</a>
</div>
</div>
</div>