这是一个高级Java面试系列题中的第一部分。这一部分论述了可变参数,断言,垃圾回收,初始化器,令牌化,日期,日历等等Java核心问题。
可变参数允许调用参数数量不同的方法。请看下面例子中的求和方法。此方法可以调用1个int参数,或2个int参数,或多个int参数。
Syntax of a variable argument.
(... numbers) {
sum = ;
( number: numbers) {
sum += number;
}
sum;
}
<span class="hljs-keyword" style="color:rgb(0,136);">static</span> <span class="hljs-keyword" style="color:rgb(0,136);">void</span> <span class="hljs-title">main</span>(String[] args) {
VariableArgumentExamples example = <span class="hljs-keyword" style="color:rgb(0,136);">new</span> VariableArgumentExamples();
<span class="hljs-comment" style="color:rgb(136,0);">//3 Arguments</span>
System.<span class="hljs-keyword" style="color:rgb(0,136);">out</span>.println(example.sum(<span class="hljs-number" style="color:rgb(0,102);">1</span>,<span class="hljs-number" style="color:rgb(0,102);">4</span>,102);">5</span>));<span class="hljs-comment" style="color:rgb(136,0);">//10</span>
<span class="hljs-comment" style="color:rgb(136,0);">//4 Arguments</span>
System.<span class="hljs-keyword" style="color:rgb(0,102);">5</span>,102);">20</span>));<span class="hljs-comment" style="color:rgb(136,0);">//30</span>
<span class="hljs-comment" style="color:rgb(136,0);">//0 Arguments</span>
System.<span class="hljs-keyword" style="color:rgb(0,136);">out</span>.println(example.sum());<span class="hljs-comment" style="color:rgb(136,0);">//0</span>
}</code></pre>
断言是在Java 1.4中引入的。它能让你验证假设。如果断言失败(即返回false),就会抛出AssertionError(如果启用断言)。基本断言如下所示。
( principal, interest,136);">int years){
(principal>);
;
}
断言不应该用于验证输入数据到一个public方法或命令行参数。IllegalArgumentException会是一个更好的选择。在public方法中,只用断言来检查它们根本不应该发生的情况。
垃圾回收?
垃圾回收是Java中自动内存管理的另一种叫法。垃圾回收的目的是为程序保持尽可能多的可用堆(heap)。 JVM会删除堆上不再需要从堆引用的对象。
垃圾回收?
通过函数第一行代码中参考变量calendar,在堆上创建了GregorianCalendar类的一个对象。
函数结束执行后,引用变量calendar不再有效。因此,在方法中没有创建引用到对象。
JVM认识到这一点,会从堆中删除对象。这就是所谓的垃圾回收。
垃圾回收?
垃圾回收在JVM突发奇想和心血来潮时运行(没有那么糟糕)。运行垃圾收集的可能情况是:
- 堆可用内存不足
- cpu空闲
垃圾回收的最佳做法?
用编程的方式,我们可以要求(记住这只是一个请求——不是一个命令)JVM通过调用System.gc()方法来运行垃圾回收。
当内存已满,且堆上没有对象可用于垃圾回收时,JVM可能会抛出OutOfMemoryException。
对象在被垃圾回收从堆上删除之前,会运行finalize()方法。我们建议不要用finalize()方法写任何代码。
初始化数据块——当创建对象或加载类时运行的代码。
有两种类型的初始化数据块:
静态初始化器:加载类时运行的的代码
实例初始化器:创建新对象时运行的代码
请看下面的例子:static{ 和 }之间的代码被称为静态初始化器。它只有在第一次加载类时运行。只有静态变量才可以在静态初始化器中进行访问。虽然创建了三个实例,但静态初始化器只运行一次。
InitializerExamples {
count;
i;
<span class="hljs-keyword" style="color:rgb(0,136);">static</span>{
<span class="hljs-comment" style="color:rgb(136,0);">//This is a static initializers. Run only when Class is first loaded.</span>
<span class="hljs-comment" style="color:rgb(136,0);">//Only static variables can be accessed</span>
System.<span class="hljs-keyword" style="color:rgb(0,136);">out</span>.println(<span class="hljs-string" style="color:rgb(0,136,0);">"Static Initializer"</span>);
<span class="hljs-comment" style="color:rgb(136,0);">//i = 6;//COMPILER ERROR</span>
System.<span class="hljs-keyword" style="color:rgb(0,0);">"Count when Static Initializer is run is "</span> + count);
}
<span class="hljs-keyword" style="color:rgb(0,136);">void</span> <span class="hljs-title">main</span>(String[] args) {
InitializerExamples example = <span class="hljs-keyword" style="color:rgb(0,136);">new</span> InitializerExamples();
InitializerExamples example2 = <span class="hljs-keyword" style="color:rgb(0,136);">new</span> InitializerExamples();
InitializerExamples example3 = <span class="hljs-keyword" style="color:rgb(0,136);">new</span> InitializerExamples();
}
}
示例输出
Initializer
Count Initializer run
让我们来看一个例子:每次创建类的实例时,实例初始化器中的代码都会运行。
i;
{
System.);
i = ;
count = count + ;
System. + count);
}
<span class="hljs-keyword" style="color:rgb(0,136);">new</span> InitializerExamples();
InitializerExamples example1 = <span class="hljs-keyword" style="color:rgb(0,monospace;color:inherit;display:block;">Instance Initializer
Count <span class="hljs-keyword" style="color:rgb(0,136);">when</span> Instance Initializer <span class="hljs-keyword" style="color:rgb(0,102);">1</span>
Instance Initializer
Count <span class="hljs-keyword" style="color:rgb(0,102);">2</span>
Instance Initializer
Count <span class="hljs-keyword" style="color:rgb(0,102);">3</span></code></pre>
正则表达式能让解析、扫描和分割字符串变得非常容易。Java中常用的正则表达式——Patter,Matcher和Scanner类。
令牌化是指在分隔符的基础上将一个字符串分割为若干个子字符串。例如,分隔符;分割字符串ac;bd;def;e为四个子字符串ac,bd,def和e。
分隔符自身也可以是一个常见正则表达式。
String.split(regex)函数将regex作为参数。
(String ,String regex) {
String[] tokens = .split(regex);
System..println(Arrays.toString(tokens));
}
tokenize(<span class="hljs-string" style="color:rgb(0,0);">"ac;bd;def;e"
,<span class="hljs-string" style="color:rgb(0,0);">";");<span class="hljs-comment" style="color:rgb(136,0);">//[ac,bd,def,e]
static tokenizeUsingScanner( , regex) {
Scanner scanner Scanner();
scanneruseDelimiter(regex);
matches ArrayList();
(scannerhasNext()){
matchesadd(scannernext());
}
Systemoutprintln(matches);
}
tokenizeUsingScanner(<span class="hljs-string" style="color:rgb(0,e]
添加小时(hour)到一个日期对象(Date Objects)?
现在,让我们如何看看添加小时到一个date对象。所有在date上的日期操作都需要通过添加毫秒到date才能完成。例如,如果我们想增加6个小时,那么我们需要将6小时换算成毫秒。6小时= 6 * 60 * 60 * 1000毫秒。请看以下的例子。
();
<span class="hljs-comment" style="color:rgb(136,0);">//Increase time by 6 hrs
<span class="hljs-built_in" style="color:rgb(102,102);">date<span class="hljs-built_in" style="color:rgb(102,102);">.setTime(<span class="hljs-built_in" style="color:rgb(102,102);">.
getTime() <span class="hljs-subst" style="color:rgb(0,0);">+ <span class="hljs-number" style="color:rgb(0,102);">6 <span class="hljs-subst" style="color:rgb(0,0);">* <span class="hljs-number" style="color:rgb(0,102);">60 <span class="hljs-subst" style="color:rgb(0,102);">1000);
System<span class="hljs-built_in" style="color:rgb(102,102);">.
println(<span class="hljs-built_in" style="color:rgb(102,102);">date);
<span class="hljs-comment" style="color:rgb(136,0);">//Decrease time by 6 hrs
<span class="hljs-built_in" style="color:rgb(102,102);">Date();
<span class="hljs-built_in" style="color:rgb(102,0);">- <span class="hljs-number" style="color:rgb(0,102);">date);
格式化日期需要使用DateFormat类完成。让我们看几个例子。
(DateFormat()(
date))
带有区域设置的格式化日期如下所示:
(
DateFormat,new Locale(,0);">"IT"))
(date))
System<span class="hljs-preprocessor" style="color:rgb(68,Locale<span class="hljs-preprocessor" style="color:rgb(68,68);">.ITALIAN
)
<span class="hljs-preprocessor" style="color:rgb(68,0);">;//marted“ 16 ottobre 2012
//This uses default locale US
System<span class="hljs-preprocessor" style="color:rgb(68,68);">.FULL
)<span class="hljs-preprocessor" style="color:rgb(68,0);">;//Tuesday,October 16,2012
System<span class="hljs-preprocessor" style="color:rgb(68,68);">.getDateInstance()
<span class="hljs-preprocessor" style="color:rgb(68,0);">;//Oct 16,2012
System<span class="hljs-preprocessor" style="color:rgb(68,68);">.SHORT)<span class="hljs-preprocessor" style="color:rgb(68,0);">;//10/16/12
System<span class="hljs-preprocessor" style="color:rgb(68,68);">.MEDIUM)<span class="hljs-preprocessor" style="color:rgb(68,68);">.LONG)<span class="hljs-preprocessor" style="color:rgb(68,0);">;//October 16,2012
Calendar类,在Java中用于处理日期。Calendar类提供了增加和减少天数、月数和年数的简便方法。它还提供了很多与日期有关的细节(这一年的哪一天?哪一周?等等)
获取日历类(Calendar Class)的实例?
Calendar类不能通过使用new Calendar创建。得到Calendar类实例的最好办法是在Calendar中使用getInstance() static方法。
=
方法?
在Calendar对象上设置日(day),月(month)或年(year)不难。对Day,Month或Year调用恰当Constant的set方法。下一个参数就是值。
sql" style="font-family:'Source Code Pro',monospace;color:inherit;display:block;">calendar.(Calendar.,102);">24);
calendar.,102);">8);//8 - September
calendar.,102);">2010);
calendar get方法
要获取一个特定日期的信息——2010年9月24日。我们可以使用calendar get方法。已被传递的参数表示我们希望从calendar中获得的值—— 天或月或年或……你可以从calendar获取的值举例如下:
(calendar(Calendar))
System))
System))
System_OF_MONTH))
System_OF_YEAR))
System_OF_YEAR))
System()) Calendar.SUNDAY
数字格式化类(Number Format Class)的用途?
数字格式用于格式化数字到不同的区域和不同格式中。
使用默认语言环境的数字格式
(NumberFormat(f))
使用区域设置的数字格式
使用荷兰语言环境格式化数字:
(new Locale())4032.3f))
使用德国语言环境格式化数字:
(Locale)()40324.31f))
使用区域设置格式化货币
使用荷兰语言环境格式化货币:
(new Locale(