Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在參數(shù)上加0.5然后進(jìn)行取整。
Java5以前switch(expression)中,expression只能是byte、short、char、int,嚴(yán)格意義上來(lái)講Java5以前只支持int,之所以能使用byte short char是因?yàn)榇嬖谧詣?dòng)類(lèi)型轉(zhuǎn)換。從 Java 5 開(kāi)始,Java中引入了枚舉類(lèi)型,expression也可以是 enum 類(lèi)型。從 Java 7 開(kāi)始,expression還可以是字符串(String),但是長(zhǎng)整型(long)在目前所有的版本中都是不可以的。
數(shù)組沒(méi)有l(wèi)ength()方法,而是有l(wèi)ength屬性。String有l(wèi)ength()方法。JavaScript 中,獲得字符串的長(zhǎng)度是通過(guò)length屬性得到的,這一點(diǎn)容易和Java混淆。
Java平臺(tái)提供了兩種類(lèi)型的字符串:String 和 StringBuffer/StringBuilder,它們都可以?xún)?chǔ)存和操作字符串,區(qū)別如下:
● String 是只讀字符串,也就意味著 String 引用的字符串內(nèi)容是不能被改變的。初學(xué)者可能會(huì)有這樣的誤解:
String str = “abc”;
str = “bcd”;
如上,字符串 str 明明是可以改變的呀!其實(shí)不然,str 僅僅是一個(gè)引用對(duì)象,它指向一個(gè)字符串對(duì)象“abc”。第二行代碼的含義是讓 str 重新指向了一個(gè)新的字符串“bcd”對(duì)象,而“abc”對(duì)象并沒(méi)有任何改變,只不過(guò)對(duì)象”abc”已經(jīng)沒(méi)有引用指向它了。
● StringBuffer/StringBuilder 表示的字符串對(duì)象可以直接進(jìn)行修改。
● StringBuilder是Java5中引入的,它和StringBuffer的方法完全相同,區(qū)別在于它是在單線程環(huán)境下使用的,因?yàn)樗乃蟹椒ǘ紱](méi)有被 synchronized 修飾,因此它的效率理論上也比 StringBuffer要高。
class StringEqualTest {
public static void main(String[] args) {
String s1 = "Programming";
String s2 = new String("Programming");
String s3 = "Program";
String s4 = "ming";
String s5 = "Program" + "ming";
String s6 = s3 + s4;
System.out.println(s1 == s2); //false
System.out.println(s1 == s5); //true
System.out.println(s1 == s6); //false
System.out.println(s1 == s6.intern()); //true
System.out.println(s2 == s2.intern()); //false
}
}
補(bǔ)充:解答上面的面試題需要知道如下兩個(gè)知識(shí)點(diǎn):
● String 對(duì)象的 intern()方法會(huì)得到字符串對(duì)象在常量池中對(duì)應(yīng)的版本的引用(如果常量池中有一個(gè)字符串與String 對(duì)象的 equals 結(jié)果是 true),如果常量池中沒(méi)有對(duì)應(yīng)的字符串,則該字符串將被添加到常量池中,然后返回常量池中字符串的引用;
● 字符串的+操作其本質(zhì)是創(chuàng)建了 StringBuilder 對(duì)象進(jìn)行 append 操作,然后將拼接后的 StringBuilder 對(duì)象用 toString 方法處理成 String 對(duì)象,這一點(diǎn)可以用 javap -c StringEqualTest.class 命令獲得 class 文件對(duì)應(yīng)的 JVM 字節(jié)碼指令就可以看出來(lái)。
import java.time.LocalDateTime;
import java.util.Calendar;
class DateTimeTest {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH)); // 0 - 11
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.HOUR_OF_DAY));
System.out.println(cal.get(Calendar.MINUTE));
System.out.println(cal.get(Calendar.SECOND));
// Java 8
LocalDateTime dt = LocalDateTime.now();
System.out.println(dt.getYear());
System.out.println(dt.getMonthValue()); // 1 - 12
System.out.println(dt.getDayOfMonth());
System.out.println(dt.getHour());
System.out.println(dt.getMinute());
System.out.println(dt.getSecond());
}
}
class GetTime {
public static void main(String[] args) {
System.out.println("第一種:" + Calendar.getInstance().getTimeInMillis());
System.out.println("第二種:" + System.currentTimeMillis());
System.out.println("第三種:" + Clock.systemDefaultZone().millis());
}
}
class GetLastDay {
public static void main(String[] args) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//獲取當(dāng)前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);//設(shè)置為 1 號(hào),當(dāng)前日期既為本月第一天
String first = format.format(c.getTime());
System.out.println("first:" + first);
//獲取當(dāng)前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last = format.format(ca.getTime());
System.out.println("last:" + last);
//Java 8
LocalDate today = LocalDate.now();
//本月的第一天
LocalDate firstday = LocalDate.of(today.getYear(), today.getMonth(), 1);
//本月的最后一天
LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月的第一天" + firstday);
System.out.println("本月的最后一天" + lastDay);
}
}
運(yùn)行結(jié)果:
first:2019-04-01
last:2019-04-30
本月的第一天2019-04-01
本月的最后一天2019-04-30
java.text.DataFormat的子類(lèi)(如 SimpleDateFormat 類(lèi))中的 format(Date)方法可將日期格式化。Java 8 中可以用 java.time.format.DateTimeFormatter 來(lái)格式化時(shí)間日期,代碼如下所示:
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
class DateFormatTest {
public static void main(String[] args) {
SimpleDateFormat oldFormatter = new SimpleDateFormat("yyyy/MM/dd");
Date date1 = new Date();
System.out.println(oldFormatter.format(date1));
// Java 8
DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate date2 = LocalDate.now();
System.out.println(date2.format(newFormatter));
}
}
運(yùn)行結(jié)果:
2019/04/22
2019/04/22
補(bǔ)充:Java 的時(shí)間日期 API 一直以來(lái)都是被詬病的東西,為了解決這一問(wèn)題,Java 8 中引入了新的時(shí)間日期 API, 其中包括 LocalDate、LocalTime、LocalDateTime、Clock、Instant 等類(lèi),這些的類(lèi)的設(shè)計(jì)都使用了不變模式,因此是線程安全的設(shè)計(jì)。
import java.util.Calendar;
class YesterdayCurrent {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
System.out.println(cal.getTime());
}
}
//java-8
import java.time.LocalDateTime;
class YesterdayCurrent {
public static void main(String[] args) {
LocalDateTime today = LocalDateTime.now();
LocalDateTime yesterday = today.minusDays(1);
System.out.println(yesterday);
}
}
其實(shí) JSR310 的規(guī)范領(lǐng)導(dǎo)者 Stephen Colebourne,同時(shí)也是 Joda-Time 的創(chuàng)建者,JSR310 是在 Joda-Time 的基礎(chǔ)上建立的,參考了絕大部分的 API,但并不是說(shuō) JSR310=JODA-Time,下面幾個(gè)比較明顯的區(qū)別是:
● 最明顯的變化就是包名(從 org.joda.time 以及 java.time)
● JSR310 不接受 NULL 值,Joda-Time 視 NULL 值為 0
● JSR310 的計(jì)算機(jī)相關(guān)的時(shí)間(Instant)和與人類(lèi)相關(guān)的時(shí)間(DateTime)之間的差別變得更明顯
● JSR310 所有拋出的異常都是 DateTimeException 的子類(lèi)。雖然 DateTimeException 是一個(gè)RuntimeException。