Sometime back I wrote an article with 8 java tricky programming questions and my friends liked it a lot. Today we will look into some java tricky interview questions.

有时我写了一篇有关8个Java棘手的编程问题的文章,我的朋友们非常喜欢它。 今天,我们将研究一些Java棘手的面试问题。

Java棘手面试问题 (Java Tricky Interview Questions)

Recently I got two java questions that I will be explaining here.

最近,我有两个Java问题,将在这里解释。

Java棘手面试问题1 (Java Tricky Interview Question 1)

What is the output of the below program?

以下程序的输出是什么?

public class Test {public static void main(String[] args) {foo(null);}public static void foo(Object o) {System.out.println("Object impl");}public static void foo(String s) {System.out.println("String impl");}
}

Java Tricky编程问题2 (Java Tricky Programming Question 2)

What will below statements print?

下面的语句将打印什么?

long longWithL = 1000*60*60*24*365L;
long longWithoutL = 1000*60*60*24*365;
System.out.println(longWithL);
System.out.println(longWithoutL);

*****************************

*****************************

Java Tricky面试问题1答案及解释 (Java Tricky Interview Question 1 Answer with Explanation)

As we know that we can assign null to any object, so doesn’t compiler complains about this program? According to java specs, in case of overloading, compiler picks the most specific function. Obviously String class is more specific than Object class, hence it will print “String impl”.
What if we have another method in the class like below:

我们知道可以为任何对象分配null,因此编译器不会抱怨该程序吗? 根据Java规范,在发生重载的情况下,编译器会选择最具体的函数 。 显然,String类比Object类更具体,因此它将打印“ String impl”。
如果我们在类中有另一个方法,如下所示:

public static void foo(StringBuffer i){System.out.println("StringBuffer impl");}

In this case, java compiler will throw an error as “The method foo(String) is ambiguous for the type Test” because String and StringBuffer, none of them are more specific to others. A method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. We can pass String as a parameter to Object argument and String argument but not to StringBuffer argument method.

在这种情况下,Java编译器将引发错误,因为“方法foo(String)对于Test类型是模棱两可的”,因为String和StringBuffer都不是其他类型的。 如果第一个方法处理的任何调用都可以传递给另一个方法而没有编译时类型错误,则该方法比另一个方法更具体。 我们可以将String作为参数传递给Object参数和String参数,但不能传递给StringBuffer参数方法。

Java Tricky编程问题2答案及解释 (Java Tricky Programming Question 2 Answer with Explanation)

The output of the code snippet will be:

代码段的输出将是:

31536000000
1471228928

In case of the first variable, we are explicitly creating it as long by placing an “L” at the end, so the compiler will treat this at long and assign it to the first variable.

对于第一个变量,只要在结尾处加一个“ L”,就可以显式地创建它,因此编译器将对其进行长时间处理并将其分配给第一个变量。

In the second case, the compiler will do the calculation and treat it as a 32-bit integer, since the output is outside the range of integer max value (2147483647), the compiler will truncate the most significant bits and then assign it to the variable.

在第二种情况下,编译器将进行计算并将其视为32位整数,因为输出超出了整数最大值(2147483647)的范围,因此编译器将截断最高有效位,然后将其分配给变量。

Binary equivalent of 1000*60*60*24*365L = 011101010111101100010010110000000000 (36 bits)
Removing 4 most significant bits to accommodate in 32-bit int, value = 01010111101100010010110000000000 (32 bits)
Which is equal to 1471228928 and hence the output.

二进制等效项1000 * 60 * 60 * 24 * 365L = 011101010111101100010010110000000000(36位)
删除4个最高有效位以容纳32位int,值= 01010111101100010010110000000000(32位)
它等于1471228928,因此等于输出。

Recently I have created YouTube videos for java tricky programs, you should check them out. Also subscribe to my YouTube Channel to get notified when I add new videos.

最近,我为Java棘手程序创建了YouTube视频,您应该将其检出。 同时订阅我的YouTube频道,以在添加新视频时得到通知。

演示地址

GitHub Repository.GitHub Repository中检出更多Java示例程序。

翻译自: https://www.journaldev.com/552/java-tricky-interview-questions

Java棘手面试问题相关推荐

  1. 一个很艰难的 Java 核心面试问题!

    一个很艰难的 Java 核心面试问题,这个 Java 问题也常被问: 什么是线程安全的单例,你怎么创建它. 好吧,在Java 5之前的版本, 使用双重检查锁定创建单例 Singleton 时,如果多个 ...

  2. java初学者面试_Java面试的前50个问题,面向初学者和经验丰富的程序员

    java初学者面试 您可以参加任何Java面试,无论是大四还是中级,经验或新来的人,一定会看到线​​程,并发和多线程中的几个问题. 实际上,这种内置的并发支持是Java编程语言的最强优势之一,并帮助它 ...

  3. java泛型面试_Java泛型面试问题

    java泛型面试 Java面试中的通用面试问题在相当长的时间内在Java 5周围越来越普遍,许多应用程序都转移到Java 5上,并且几乎所有新的Java开发都发生在Tiger(Java 5的代号)上. ...

  4. java 字符串面试_Java字符串面试问答

    java 字符串面试 String is one of the most widely used Java Class. Here I am listing some important Java S ...

  5. java技术面试之面试题大全

    转载自:http://blog.csdn.net/lijizhi19950123/article/details/77679489 Java 面试知识点总结 本篇文章会对面试中常遇到的Java技术点进 ...

  6. 2022 年 25 大 Java 8 面试问题和答案 - 从基础到有经验

    文章目录 什么是 Java 8? Java 8 面试问题 - 基础级别 1.Java 8 引入了哪些新特性? 2. 为什么首先需要新版本的 Java? 3. 那么,Java 8 带来了哪些实际优势? ...

  7. java培训面试技巧分享

    很多人在学会java技术之后,就开始筹备自己的面试了,java技术在互联网行业的需求是很大的,所以内卷是很严重的,在面试环节一定要全力以赴才行,下面小编就教大家一些java培训面试技巧,希望能帮助到大 ...

  8. 技术直播:1小时突击Java工程师面试核心(限免报名)

    后疫情时代,连程序员这个多金的职业也遭受到了一定程度的打击.从各大招聘网站和多次面试经历中,相信大家已经意识到,面试官对程序员技能体系和项目经验考核似乎更严苛了.你在面试中常常为什么苦恼呢?简历撰写? ...

  9. 杭州中国移动java待遇_【中国移动杭州研发中心Java面试】移动杭研社招java中级面试-看准网...

    移动杭研社招java中级面试 移动杭研的面试比较紧凑,一个上午就面完了.到了后先在前台登记,填写个人信息,和党性测试,党性测试有点像以前点政治题,知道不知道随意填了下,前台妹子说这个不要紧的.然后一面 ...

最新文章

  1. Java 9 - 17 特性解读:Java 11
  2. Build与Version
  3. 布隆过滤器Redis缓存穿透雪崩击穿热点key
  4. C++ stringstream输入方式
  5. 明年5G智能手机大爆发!出货量惊人
  6. 【Janino】Janino框架初识与使用教程
  7. 宝塔面板 php关闭拓展,宝塔Linux面板中PHP如何安装扩展及禁用函数?
  8. 页面布局让footer居页面底部_网站页面结构与关键词布局技巧
  9. paip.python开发环境搭建
  10. Java企业级实战项目
  11. 富其云ERP学习笔记
  12. ActiveMQ失效转移(Failover)
  13. 如何提取伴奏?1分钟让你知道伴奏提取软件手机版有哪些
  14. 网络共享查看其他计算机取消密码,Win7局域网访问需要密码 win7取消共享密码的方法...
  15. 圣杯布局原来这么简单!!
  16. matlab使用hough变换函数进行车道检测
  17. 密码的修改(首先获取该用户的id,原密码判断是否一致,新密码和再次输入密码判断是否一样)...
  18. openwrt 编程器固件制作方法
  19. 如何破解Amazon 登陆 metadata1值?Amazon 登陆 metadata1 形成的主要混淆的js研究
  20. 计算机基础 第一章 计算机网络概述 知识点总结

热门文章

  1. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
  2. shell(九)几个字符转换命令
  3. 对SIL9022/9024的配置
  4. Pro Git 读书笔记
  5. mysql数据类型范围导致失败
  6. 使用特殊字体实现特殊报表效果
  7. [转载] Python: fnmatch模块 (Unix B-Shell通配符的文件名匹配)
  8. JVM01----JVM结构
  9. 生活小记--工作一年后的菜鸡
  10. ajax+MultipartFile上传文件到本地