高階Java面試題的學習掌握對於面試者來說是很佔優勢的,下面小編給大家介紹經典Java基礎面試題,歡迎閱讀!
高階Java面試題彙總這是一個高階Java面試系列題中的第一部分。這一部分論述了可變引數,斷言,垃圾回收,初始化器,令牌化,日期,日曆等等Java核心問題。
什麼是可變引數?
可變引數允許呼叫引數數量不同的方法。請看下面例子中的求和方法。此方法可以呼叫1個int引數,或2個int引數,或多個int引數。
//int(type) followed ... (three dot's) is syntax of a variable argument.
public int sum(int... numbers) {
//inside the method a variable argument is similar to an array.
//number can be treated as if it is declared as int[] numbers;
int sum = 0;
for (int number: numbers) {
sum += number;
}
return sum;
}
public static void main(String[] args) {
VariableArgumentExamples example = new VariableArgumentExamples();
//3 Arguments
tln((1, 4, 5));//10
//4 Arguments
tln((1, 4, 5, 20));//30
//0 Arguments
tln(());//0
}
斷言的用途?
斷言是在Java 1.4中引入的。它能讓你驗證假設。如果斷言失敗(即返回false),就會丟擲AssertionError(如果啟用斷言)。基本斷言如下所示。
private int computerSimpleInterest(int principal,float interest,int years){
assert(principal>0);
return 100;
}
什麼時候使用斷言?
斷言不應該用於驗證輸入資料到一個public方法或命令列引數。IllegalArgumentException會是一個更好的選擇。在public方法中,只用斷言來檢查它們根本不應該發生的情況。
什麼是垃圾回收?
垃圾回收是Java中自動記憶體管理的另一種叫法。垃圾回收的目的是為程式保持儘可能多的可用堆(heap)。 JVM會刪除堆上不再需要從堆引用的物件。
用一個例子解釋垃圾回收?
比方說,下面這個方法就會從函式呼叫。
void method(){
Calendar calendar = new GregorianCalendar(2000,10,30);
tln(calendar);
}
通過函式第一行程式碼中參考變數calendar,在堆上建立了GregorianCalendar類的一個物件。
函式結束執行後,引用變數calendar不再有效。因此,在方法中沒有建立引用到物件。
JVM認識到這一點,會從堆中刪除物件。這就是所謂的垃圾回收。
什麼時候執行垃圾回收?
垃圾回收在JVM突發奇想和心血來潮時執行(沒有那麼糟糕)。執行垃圾收集的可能情況是:
堆可用記憶體不足
CPU空閒
垃圾回收的.最佳做法?
用程式設計的方式,我們可以要求(記住這只是一個請求——不是一個命令)JVM通過呼叫()方法來執行垃圾回收。
當記憶體已滿,且堆上沒有物件可用於垃圾回收時,JVM可能會丟擲OutOfMemoryException。
物件在被垃圾回收從堆上刪除之前,會執行finalize()方法。我們建議不要用finalize()方法寫任何程式碼。
什麼是初始化資料塊?
初始化資料塊——當建立物件或載入類時執行的程式碼。
有兩種型別的初始化資料塊:
靜態初始化器:載入類時執行的的程式碼
例項初始化器:建立新物件時執行的程式碼
什麼是靜態初始化器?
請看下面的例子:static{ 和 }之間的程式碼被稱為靜態初始化器。它只有在第一次載入類時執行。只有靜態變數才可以在靜態初始化器中進行訪問。雖然建立了三個例項,但靜態初始化器只執行一次。
public class InitializerExamples {
static int count;
int i;
static{
//This is a static initializers. Run only when Class is first loaded.
//Only static variables can be accessed
tln("Static Initializer");
//i = 6;//COMPILER ERROR
tln("Count when Static Initializer is run is " + count);
}
public static void main(String[] args) {
InitializerExamples example = new InitializerExamples();
InitializerExamples example2 = new InitializerExamples();
InitializerExamples example3 = new InitializerExamples();
}
}
示例輸出
Static Initializer
Count when Static Initializer is run is 0.
什麼是例項初始化塊?
讓我們來看一個例子:每次建立類的例項時,例項初始化器中的程式碼都會執行。
public class InitializerExamples {
static int count;
int i;
{
//This is an instance initializers. Run every time an object is created.
//static and instance variables can be accessed
tln("Instance Initializer");
i = 6;
count = count + 1;
tln("Count when Instance Initializer is run is " + count);
}
public static void main(String[] args) {
InitializerExamples example = new InitializerExamples();
InitializerExamples example1 = new InitializerExamples();
InitializerExamples example2 = new InitializerExamples();
}
}
示例輸出
Instance Initializer
Count when Instance Initializer is run is 1
Instance Initializer
Count when Instance Initializer is run is 2
Instance Initializer
Count when Instance Initializer is run is 3
什麼是正則表示式?
正則表示式能讓解析、掃描和分割字串變得非常容易。Java中常用的正則表示式——Patter,Matcher和Scanner類。
什麼是令牌化?
令牌化是指在分隔符的基礎上將一個字串分割為若干個子字串。例如,分隔符;分割字串ac;bd;def;e為四個子字串ac,bd,def和e。
分隔符自身也可以是一個常見正則表示式。
t(regex)函式將regex作為引數。
給出令牌化的例子?
private static void tokenize(String string,String regex) {
String[] tokens = t(regex);
tln(ring(tokens));
}
tokenize("ac;bd;def;e",";");//[ac, bd, def, e]
如何使用掃描器類(Scanner Class)令牌化?
private static void tokenizeUsingScanner(String string,String regex) {
Scanner scanner = new Scanner(string);
elimiter(regex);
List<String> matches = new ArrayList<String>();
while(ext()){
(());
}
tln(matches);
}
tokenizeUsingScanner("ac;bd;def;e",";");//[ac, bd, def, e]
如何新增小時(hour)到一個日期物件(Date Objects)?
現在,讓我們如何看看新增小時到一個date物件。所有在date上的日期操作都需要通過新增毫秒到date才能完成。例如,如果我們想增加6個小時,那麼我們需要將6小時換算成毫秒。6小時= 6 * 60 * 60 * 1000毫秒。請看以下的例子。
Date date = new Date();
//Increase time by 6 hrs
ime(ime() + 6 * 60 * 60 * 1000);
tln(date);
//Decrease time by 6 hrs
date = new Date();
ime(ime() - 6 * 60 * 60 * 1000);
tln(date);
如何格式化日期物件?
格式化日期需要使用DateFormat類完成。讓我們看幾個例子。
//Formatting Dates
tln(nstance()at(
date));//10/16/12 5:18 AM
帶有區域設定的格式化日期如下所示:
tln(ateInstance(
, new Locale("it", "IT"))
at(date));//marted“ 16 ottobre 2012
tln(ateInstance(
, IAN)
at(date));//marted“ 16 ottobre 2012
//This uses default locale US
tln(ateInstance(
)at(date));//Tuesday, October 16, 2012
tln(ateInstance()
at(date));//Oct 16, 2012
tln(ateInstance(
T)at(date));//10/16/12
tln(ateInstance(
UM)at(date));//Oct 16, 2012
tln(ateInstance(
)at(date));//October 16, 2012
Java中日曆類(Calendar Class)的用途?
Calendar類在Java中用於處理日期。Calendar類提供了增加和減少天數、月數和年數的簡便方法。它還提供了很多與日期有關的細節(這一年的哪一天?哪一週?等等)
如何在Java中獲取日曆類(Calendar Class)的例項?
Calendar類不能通過使用new Calendar建立。得到Calendar類例項的最好辦法是在Calendar中使用getInstance() static方法。
//Calendar calendar = new Calendar(); //COMPILER ERROR
Calendar calendar = nstance();
解釋一些日曆類(Calendar Class)中的重要方法?
在Calendar物件上設定日(day),月(month)或年(year)不難。對Day,Month或Year呼叫恰當Constant的set方法。下一個引數就是值。
(, 24);
(H, 8);//8 - September
(, 2010);
calendar get方法
要獲取一個特定日期的資訊——2010年9月24日。我們可以使用calendar get方法。已被傳遞的引數表示我們希望從calendar中獲得的值—— 天或月或年或……你可以從calendar獲取的值舉例如下:
tln(());//2010
tln((H));//8
tln(());//24
tln((_OF_MONTH));//4
tln((_OF_YEAR));//39
tln((_OF_YEAR));//267
tln(irstDayOfWeek());//1 -> AY
數字格式化類(Number Format Class)的用途?
數字格式用於格式化數字到不同的區域和不同格式中。
使用預設語言環境的數字格式
tln(nstance()at(321.24f));//321.24
使用區域設定的數字格式
使用荷蘭語言環境格式化數字:
tln(nstance(new Locale("nl"))at(4032.3f));//4.032,3
使用德國語言環境格式化數字: tln(nstance(ANY)at(4032.3f));//4.032,3
使用預設語言環境格式化貨幣tln(urrencyInstance()at(40324.31f));//$40,324.31
使用區域設定格式化貨幣
使用荷蘭語言環境格式化貨幣:
tln(urrencyInstance(new Locale("nl"))at(40324.31f));//? 40.324,31
許可證
這篇文章,以及任何相關的原始碼和檔案,依據The Code Project Open License (CPOL)。