20个高级Java面试题汇总

转载自  20个高级Java面试题汇总

译文链接:http://www.codeceo.com/article/20-java-advanced-interview-questions.html

英文原文:Advanced Java Interview Questions

翻译作者:码农网 – 小峰

这是一个高级Java面试系列题中的部分。这一部分论述了可变参数,断言,垃圾回收,初始化器,令牌化,日期,日历等等Java核心问题。

程序员面试指南:https://www.youtube.com/watch?v=0xcgzUdTO5M

Java面试问题集合指南:https://www.youtube.com/watch?v=GnR4hCvEIJQ

  1. 什么是可变参数?

  2. 断言的用途?

  3. 什么时候使用断言?

  4. 什么是垃圾回收?

  5. 用一个例子解释垃圾回收?

  6. 什么时候运行垃圾回收?

  7. 垃圾回收的最佳做法?

  8. 什么是初始化数据块?

  9. 什么是静态初始化器?

  10. 什么是实例初始化块?

  11. 什么是正则表达式?

  12. 什么是令牌化?

  13. 给出令牌化的例子?

  14. 如何使用扫描器类(Scanner Class)令牌化?

  15. 如何添加小时(hour)到一个日期对象(Date Objects)?

  16. 如何格式化日期对象?

  17. Java中日历类(Calendar Class)的用途?

  18. 如何在Java中获取日历类的实例?

  19. 解释一些日历类中的重要方法?

  20. 数字格式化类(Number Format Class)的用途?

 

什么是可变参数?

可变参数允许调用参数数量不同的方法。请看下面例子中的求和方法。此方法可以调用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 ArgumentsSystem.out.println(example.sum(1, 4, 5));//10//4 ArgumentsSystem.out.println(example.sum(1, 4, 5, 20));//30//0 ArgumentsSystem.out.println(example.sum());//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);System.out.println(calendar);
}

通过函数第一行代码中参考变量calendar,在堆上创建了GregorianCalendar类的一个对象。

函数结束执行后,引用变量calendar不再有效。因此,在方法中没有创建引用到对象。

JVM认识到这一点,会从堆中删除对象。这就是所谓的垃圾回收。

 

什么时候运行垃圾回收?

垃圾回收在JVM突发奇想和心血来潮时运行(没有那么糟糕)。运行垃圾收集的可能情况是:

  • 堆可用内存不足

  • CPU空闲

 

垃圾回收的最佳做法?

用编程的方式,我们可以要求(记住这只是一个请求——不是一个命令)JVM通过调用System.gc()方法来运行垃圾回收。

当内存已满,且堆上没有对象可用于垃圾回收时,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 accessedSystem.out.println("Static Initializer");//i = 6;//COMPILER ERRORSystem.out.println("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 accessedSystem.out.println("Instance Initializer");i = 6;count = count + 1;System.out.println("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 InitializerCount when Instance Initializer is run is 1Instance InitializerCount when Instance Initializer is run is 2Instance InitializerCount when Instance Initializer is run is 3

 

什么是正则表达式?

正则表达式能让解析、扫描和分割字符串变得非常容易。Java中常用的正则表达式——Pattern,Matcher和Scanner类。

 

什么是令牌化?

令牌化是指在分隔符的基础上将一个字符串分割为若干个子字符串。例如,分隔符;分割字符串ac;bd;def;e为四个子字符串ac,bd,def和e。

分隔符自身也可以是一个常见正则表达式。

String.split(regex)函数将regex作为参数。

 

给出令牌化的例子?

private static void tokenize(String string,String regex) {String[] tokens = string.split(regex);System.out.println(Arrays.toString(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);scanner.useDelimiter(regex);List<String> matches = new ArrayList<String>();while(scanner.hasNext()){matches.add(scanner.next());}System.out.println(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
date.setTime(date.getTime() + 6 * 60 * 60 * 1000);
System.out.println(date);//Decrease time by 6 hrs
date = new Date();
date.setTime(date.getTime() - 6 * 60 * 60 * 1000);
System.out.println(date);

 

如何格式化日期对象?

格式化日期需要使用DateFormat类完成。让我们看几个例子。

//Formatting Dates
System.out.println(DateFormat.getInstance().format(date));//10/16/12 5:18 AM

带有区域设置的格式化日期如下所示:

System.out.println(DateFormat.getDateInstance(DateFormat.FULL, new Locale("it", "IT")).format(date));
//marted&ldquo; 16 ottobre 2012System.out.println(DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALIAN).format(date));
//marted&ldquo; 16 ottobre 2012//This uses default locale US
System.out.println(DateFormat.getDateInstance(DateFormat.FULL).format(date));
//Tuesday, October 16, 2012System.out.println(DateFormat.getDateInstance().format(date));
//Oct 16, 2012
System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(date));
//10/16/12
System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM).format(date));
//Oct 16, 2012System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(date));
//October 16, 2012

 

Java中日历类(Calendar Class)的用途?

Calendar类(Youtube视频链接 - https://www.youtube.com/watch?v=hvnlYbt1ve0)在Java中用于处理日期。Calendar类提供了增加和减少天数、月数和年数的简便方法。它还提供了很多与日期有关的细节(这一年的哪一天?哪一周?等等)

 

如何在Java中获取日历类(Calendar Class)的实例?

Calendar类不能通过使用new Calendar创建。得到Calendar类实例的最好办法是在Calendar中使用getInstance() static方法。

//Calendar calendar = new Calendar(); 
//COMPILER ERROR
Calendar calendar = Calendar.getInstance();

 

解释一些日历类(Calendar Class)中的重要方法?

在Calendar对象上设置日(day),月(month)或年(year)不难。对Day,Month或Year调用恰当Constant的set方法。下一个参数就是值。

calendar.set(Calendar.DATE, 24);
calendar.set(Calendar.MONTH, 8);
//8 - September
calendar.set(Calendar.YEAR, 2010);

calendar get方法

要获取一个特定日期的信息——2010年9月24日。我们可以使用calendar get方法。已被传递的参数表示我们希望从calendar中获得的值—— 天或月或年或……你可以从calendar获取的值举例如下:

System.out.println(calendar.get(Calendar.YEAR));//2010
System.out.println(calendar.get(Calendar.MONTH));//8
System.out.println(calendar.get(Calendar.DATE));//24
System.out.println(calendar.get(Calendar.WEEK_OF_MONTH));//4
System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));//39
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));//267
System.out.println(calendar.getFirstDayOfWeek());
//1 -> Calendar.SUNDAY

 

数字格式化类(Number Format Class)的用途?

数字格式用于格式化数字到不同的区域和不同格式中。

使用默认语言环境的数字格式

System.out.println(NumberFormat.getInstance().
format(321.24f));//321.24

使用区域设置的数字格式

使用荷兰语言环境格式化数字:

System.out.println(NumberFormat.getInstance(
new Locale("nl")).format(4032.3f));//4.032,3

使用德国语言环境格式化数字:

System.out.println(NumberFormat.
getInstance(Locale.GERMANY).format(4032.3f));
//4.032,3

使用默认语言环境格式化货币

System.out.println(NumberFormat.getCurrencyInstance().
format(40324.31f));//$40,324.31

使用区域设置格式化货币

使用荷兰语言环境格式化货币:

System.out.println(NumberFormat.
getCurrencyInstance(new Locale("nl")).
format(40324.31f));//? 40.324,31

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/323646.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

springboot实现复杂业务下的更新操作

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注微信公众号&#xff1a;雄雄的小课堂 前言 现在是2022年5月4日19:25:55&#xff01;今天写了个这样的功能&#xff1a; 某用户在一天内有多个训练项目&#xff0c;比如&#xff1a;晨跑&#xff0c;有氧训练&#xff0c;跳绳这…

HDOJ5542-The Battle of Chibi【树状数组,dp】

正题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid5542 题目大意 求序列A有多少个长度为M的递增子序列。 解题思路 用fi,jfi,j表示长度为i&#xff0c;以AjAj结尾的序列的个数。然后显然得出动态转移方程通过上一次从任意一个地方转移&#xff0c;动态转移方程&…

ASP.NET Core中为指定类添加WebApi服务功能

POCO Controller是 ASP.NET Core 中的一个特性&#xff0c;虽然在2015年刚发布的时候就有这个特性了&#xff0c;可是大多数开发者都只是按原有的方式去写&#xff0c;而没有用到这个特性。其实&#xff0c;如果利用这个特性进行稍微封装后&#xff0c;用在SOA架构中Service层的…

elementui解决el-dialog不清空内容的问题,el-dialog关闭时销毁子组件

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注微信公众号&#xff1a;雄雄的小课堂 前言 现在是2022年5月5日22:48:21&#xff01; 今天在使用element-ui中的el-dialog的时候遇到了个这样的问题&#xff1a;页面上点击添加的按钮&#xff0c;弹出el-dialog对话框&#xff…

MySQL 中的重做日志,回滚日志以及二进制日志的简单总结

转载自 MySQL 中的重做日志&#xff0c;回滚日志以及二进制日志的简单总结 MySQL中有六种日志文件&#xff0c;分别是&#xff1a;重做日志&#xff08;redo log&#xff09;、回滚日志&#xff08;undo log&#xff09;、二进制日志&#xff08;binlog&#xff09;、错误日志…

c语言分离三位数

#include<stdio.h> main(){ int k,l,m,n;printf("请输入一个三位数"); scanf("%d",&k);lk/100;mk/10%10;nk%10;printf("这个三位数的百位是:%d\n",l);printf("这个三位数的十位是:%d\n",m);printf("这个三位数的个位是…

POJ1821-Fence【单调队列,dp】

正题 题目链接:http://poj.org/problem?id1821 题目大意 有n个木板,m个工人&#xff0c;每个木板只能被粉刷一次&#xff0c;第i个工人如果刷的话必须刷木板SiSi&#xff0c;连续的不超过LiLi的&#xff0c;没一块PiPi。 解题思路 用fi,jfi,j表示前j块木板&#xff0c;前i个…

分布式ID自增算法 Snowflake

近在尝试EF的多数据库移植&#xff0c;但是原始项目中主键用的Sqlserver的GUID。MySQL没法移植了。 其实发现GUID也没法保证数据的递增性&#xff0c;又不太想使用int递增主键&#xff0c;就开始探索别的ID形式。 后来发现twitter的Snowflake算法。 一开始我尝试过直接引用N…

java中,根据指定日期显示出前n天的日期

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注微信公众号&#xff1a;雄雄的小课堂 今天分享的是&#xff1a;在java中&#xff0c;根据指定日期显示出前n天的日期 效果如下&#xff1a; 大家注意观察上面的时间&#xff0c;我传入的时间是&#xff1a;2022年5月9日21:28:…

你真的很熟分布式和事务吗?

转载自 你真的很熟分布式和事务吗&#xff1f; 微吐槽 hello,world. 不想了&#xff0c;我等码农&#xff0c;还是看看怎么来处理分布式系统中的事务这个老大难吧&#xff01; 本文略长&#xff0c;读者需要有一定耐心&#xff0c;如果你是高级码农或者架构师级别&#xf…

线程1

模拟龟兔赛跑 线程1 package test;/*** 模拟龟兔赛跑* author Administrator**/ class Rab extends Thread{public void run() {for (int i 0; i < 100; i) {System.out.println("兔子跑了"i"步");}} }class array1 extends Thread{public void run(…

jdbc实现批量给多个表中更新数据(解析Excel表数据插入到数据库中)

大家好&#xff0c;我是雄雄&#xff0c;欢迎关注微信公众号&#xff1a;雄雄的小课堂 前言 现在是&#xff1a;2022年5月19日08:01:51 今天遇到了个这样的需求&#xff0c;解析excel表中的数据&#xff0c;以JDBC的方式&#xff0c;将数据批量更新至不同的数据表中。注意&am…

CF559C-Gerald and Giant Chess【计数类dp】

正题 上不了Codeforces&#xff0c;就用洛谷了 评测记录:https://www.luogu.org/recordnew/lists?uid52918&pidCF559C 题目大意 H∗WH∗W的棋盘上有一个卒从(1,1)走到(H,W)&#xff0c;有些点不能走&#xff0c;求方案总数。 解题思路 首先如果没有障碍走到(i,j)方案数…

.Net Core 全局配置读取管理方法 ConfigurationManager

最近在学习.Net Core的过程中&#xff0c;发现.Net Framework中常用的ConfigurationManager在Core中竟然被干掉了。 也能理解。Core中使用的配置文件全是Json&#xff0c;不像Framework使用的XML&#xff0c;暂时不支持也是能理解的&#xff0c;但是毕竟全局配置文件这种东西还…

Http 持久连接与 HttpClient 连接池

转载自 Http 持久连接与 HttpClient 连接池 一、背景 HTTP协议是无状态的协议&#xff0c;即每一次请求都是互相独立的。因此它的最初实现是&#xff0c;每一个http请求都会打开一个tcp socket连接&#xff0c;当交互完毕后会关闭这个连接。 HTTP协议是全双工的协议&#x…

jdbc解析excel文件,批量插入数据至库中

“大家好&#xff0c;我是雄雄&#xff0c;欢迎关注微信公众号&#xff1a;雄雄的小课堂”前言现在是&#xff1a;2022年5月20日09:32:38今天遇到了个这样的需求&#xff0c;解析excel表中的数据&#xff0c;以JDBC的方式&#xff0c;将数据批量更新至不同的数据表中。注意&…

线程2

public class test{/*** 测试延迟继承* param args*/public static void main(String[] args) {Cat catnew Cat();cat.start();//启动线程&#xff0c;会导致run函数的运行Dog dognew Dog();//创建一个线程对象Thread tnew Thread(dog);t.start();}} //继承Thread创建线程 clas…

.NET及.NET Core系统架构

.NET 官方架构指南 Microservices and Docker Containers Web Applications with ASP.NET 官网地址&#xff1a;https://www.microsoft.com/net/learn/architecture 三层及多层架构 Multitier Architecture ASP.NET N-Tier Architecture Schema Visual Studio N-Tier Examp…

POJ3208-Apocalypse Someday【数位dp】

正题 题目链接:http://poj.org/problem?id3208 题目大意 求第X个有3个连续的6的数。 解题思路 用fi,j(j<3)fi,j(j<3)表示i位&#xff0c;已经有j个6的方案总数。然后fi,3fi,3表示i位的魔鬼数的总数。 然后动态转移方程。 fi,09∗(fi−1,0fi−1,1fi−1,2)fi,09∗(fi…

Spring Boot 自动配置的 “魔法” 是如何实现的?

转载自 Spring Boot 自动配置的 “魔法” 是如何实现的&#xff1f; Spring Boot是Spring旗下众多的子项目之一&#xff0c;其理念是约定优于配置&#xff0c;它通过实现了自动配置&#xff08;大多数用户平时习惯设置的配置作为默认配置&#xff09;的功能来为用户快速构建出…