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 Tricky Interview Questions)

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



java 开发中比较棘手的问题_编程语言



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





(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 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 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 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