博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java棘手面试问题
阅读量:2530 次
发布时间:2019-05-11

本文共 3584 字,大约阅读时间需要 11 分钟。

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

有时我写了一篇有关8个的文章,我的朋友们非常喜欢它。 今天,我们将研究一些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:

代码段的输出将是:

315360000001471228928

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 to get notified when I add new videos.

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

. 检出更多Java示例程序。

翻译自:

转载地址:http://uklzd.baihongyu.com/

你可能感兴趣的文章
[总结]编程中遇到的vc提示的一些警告
查看>>
Python学习笔记-EXCEL操作
查看>>
9.for循环
查看>>
百度PaddlePaddle再获新技能 智能推荐、对话系统、控制领域都能搞定!
查看>>
SELINUX setsebool
查看>>
C#的Socket程序(TCP/IP)总结
查看>>
java源码生成可运行jar
查看>>
用一个常见的生活小例子来解释和演示面向接口编程
查看>>
找出数组中两个只出现一次的数字
查看>>
【TYVJ水题三连发】1502 兴建高铁 1568 Rabbit Number 1673 位图
查看>>
centos 允许远程连接mysql
查看>>
C#之用XmlWriter保存XML数据
查看>>
光和颜色
查看>>
Metro UI风格配色方案
查看>>
WGAN (原理解析)
查看>>
PHP json_encode中文乱码解决方法
查看>>
解决ie6、ie7下float为right换行的情况
查看>>
Windows搭建测试RabbitMq遇到的问题
查看>>
Net分布式系统之三:Vm安装配置Nginx
查看>>
题目搜集
查看>>