Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在參數上加0.5然后進行取整。
Java5以前switch(expression)中,expression只能是byte、short、char、int,嚴格意義上來講Java5以前只支持int,之所以能使用byte short char是因為存在自動類型轉換。從 Java 5 開始,Java中引入了枚舉類型,expression也可以是 enum 類型。從 Java 7 開始,expression還可以是字符串(String),但是長整型(long)在目前所有的版本中都是不可以的。
數組沒有length()方法,而是有length屬性。String有length()方法。JavaScript 中,獲得字符串的長度是通過length屬性得到的,這一點容易和Java混淆。
Java平臺提供了兩種類型的字符串:String 和 StringBuffer/StringBuilder,它們都可以儲存和操作字符串,區別如下:
● String 是只讀字符串,也就意味著 String 引用的字符串內容是不能被改變的。初學者可能會有這樣的誤解:
String str = “abc”;
str = “bcd”;
如上,字符串 str 明明是可以改變的呀!其實不然,str 僅僅是一個引用對象,它指向一個字符串對象“abc”。第二行代碼的含義是讓 str 重新指向了一個新的字符串“bcd”對象,而“abc”對象并沒有任何改變,只不過對象”abc”已經沒有引用指向它了。
● StringBuffer/StringBuilder 表示的字符串對象可以直接進行修改。
● StringBuilder是Java5中引入的,它和StringBuffer的方法完全相同,區別在于它是在單線程環境下使用的,因為它的所有方法都沒有被 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
}
}
補充:解答上面的面試題需要知道如下兩個知識點:
● String 對象的 intern()方法會得到字符串對象在常量池中對應的版本的引用(如果常量池中有一個字符串與String 對象的 equals 結果是 true),如果常量池中沒有對應的字符串,則該字符串將被添加到常量池中,然后返回常量池中字符串的引用;
● 字符串的+操作其本質是創建了 StringBuilder 對象進行 append 操作,然后將拼接后的 StringBuilder 對象用 toString 方法處理成 String 對象,這一點可以用 javap -c StringEqualTest.class 命令獲得 class 文件對應的 JVM 字節碼指令就可以看出來。
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");
//獲取當前月第一天:
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH, 1);//設置為 1 號,當前日期既為本月第一天
String first = format.format(c.getTime());
System.out.println("first:" + first);
//獲取當前月最后一天
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);
}
}
運行結果:
first:2019-04-01
last:2019-04-30
本月的第一天2019-04-01
本月的最后一天2019-04-30
java.text.DataFormat的子類(如 SimpleDateFormat 類)中的 format(Date)方法可將日期格式化。Java 8 中可以用 java.time.format.DateTimeFormatter 來格式化時間日期,代碼如下所示:
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));
}
}
運行結果:
2019/04/22
2019/04/22
補充:Java 的時間日期 API 一直以來都是被詬病的東西,為了解決這一問題,Java 8 中引入了新的時間日期 API, 其中包括 LocalDate、LocalTime、LocalDateTime、Clock、Instant 等類,這些的類的設計都使用了不變模式,因此是線程安全的設計。
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);
}
}
其實 JSR310 的規范領導者 Stephen Colebourne,同時也是 Joda-Time 的創建者,JSR310 是在 Joda-Time 的基礎上建立的,參考了絕大部分的 API,但并不是說 JSR310=JODA-Time,下面幾個比較明顯的區別是:
● 最明顯的變化就是包名(從 org.joda.time 以及 java.time)
● JSR310 不接受 NULL 值,Joda-Time 視 NULL 值為 0
● JSR310 的計算機相關的時間(Instant)和與人類相關的時間(DateTime)之間的差別變得更明顯
● JSR310 所有拋出的異常都是 DateTimeException 的子類。雖然 DateTimeException 是一個RuntimeException。