Java常用API面试题【Java面试题】

x33g5p2x  于2021-12-30 转载在 Java  
字(5.4k)|赞(0)|评价(0)|浏览(345)

1、Math.round(11.5)等于多少?Math.round(- 11.5) 又等于多少?

public class Test {
    public static void main(String[] args) {
        System.out.println("Math.round(11.5)="+Math.round(11.5));
        System.out.println("Math.round(-11.5)="+Math.round(-11.5));
    }
}

Math.round(11.5)的返回值是12,Math.round(-11.5)的返回值是-11。四舍五入的原理是在参数上加0.5然后进行取整。

2、switch是否能作用在byte 上,是否能作用在long上,是否能作用在String上?

Java5以前switch(expression)中,
expression只能是byte、short、char、int,严格意义上来讲Java5以前只支持int,之所以能使用byte short char是因为存在自动类型转换。

从 Java 5 开始,Java中引入了枚举类型,expression也可以是 enum 类型。

从 Java 7 开始,expression还可以是字符串(String),但是长整型(long)在目前所有的版本中都是不可以的。

3、数组有没有length()方法?String有没有length()方法?

数组没有length()方法,而是有length属性。

public class Test {
    public static void main(String[] args) {
        int a[] = {12,3,45,6,7,8};
        System.out.println(a.length);
    }
}

String有length()方法。JavaScript 中,获得字符串的长度是通过length属性得到的,这一点容易和Java混淆。

4、String 、StringBuilder 、StringBuffer 的区别?

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要高。

5、请说出下面程序的输出?

class Test {
    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 字节码指令就可以看出来。

6、如何取得年月日、小时分钟秒?

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());//秒
    }
}

7、如何取得从 1970 年 1 月 1 日 0 时 0 分 0 秒到现在的毫秒数?

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());
    }
}

8、如何取得某月的最后一天?

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);

    }
}

运行结果:

9、如何格式化日期?

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));
    }
}

运行结果:

补充:Java 的时间日期 API 一直以来都是被诟病的东西,为了解决这一问题,
Java 8 中引入了新的时间日期 API,
其中包括 LocalDate、LocalTime、LocalDateTime、Clock、Instant 等类,
这些的类的设计都使用了不变模式,因此是线程安全的设计。

10、打印昨天的当前时刻?

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);
    }
}

11、JSR310 规范 Joda-Time 的区别?

其实 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。

相关文章