This article introduces some questions frequently asked by Java beginners. Many friends are interested in whether % can be divided by a decimal? Is there any difference in the effects of a += b and a = a + b? Why does it take a lot of time to declare an array? Why does the Java library not use random pivot quick sort? If you have doubts about a series of questions, let me give you a detailed introduction through this article
This article introduces some questions frequently asked by Java beginners. Can % be divided by a decimal? Is there any difference in the effects of a += b and a = a + b? Why does declaring an array take so much time? Why doesn't the Java library use random pivot quick sort?
BasicData types
Q. Why -0/3 results in 0, while -0.0/3.0 results in Is it -0.0? (Note that the following result 0 has a negative sign)
A. In Java, integers are represented by two's complement numbers. There is only one way to represent 0 in two's complement. On the other hand, floating point numbers are represented using the IEEE standard, and there are two ways to represent 0, 0 and -0.
Q. Can I divide % by a decimal?
A. Of course. For example, if angle is a nonnegative number, then angle % (2 * Math.PI) will convert angle to a value between 0 and 2 π.
Q. When a b are both basic types variables, is there any difference in the effects of a += b and a = a + b?
A. When the types of a and b are different, the effects of the two statements may be different. a += b is equivalent to a = (int) (a + b). In this case, a can be of type int and b can be of type float. But under the same circumstances, a = a + b will compile and report an error.
Conditional statements and LoopsStatements
Q. Why judge String Can't use == for equality?
A. This reflects the difference between basic types (int, double, boolean) and reference types (String).
Q. Are there any circumstances under which the curly braces of a statement block cannot be omitted?
A. In the following example, the first piece of code is legal, and the second piece of code will cause a compilation error. From a technical point of view, that statement is a variable declaration, not a statement, so an error will be reported.
// legal for (int i = 0; i <= N; i++) { int x = 5; } // illegal for (int i = 0; i <= N; i++) int x = 5;
Q. In the following two pieces of code, are there any situations where their effects are different?
for (<init stmnt> <boolean expr>; <incr stmnt>) { <body statements> } <init stmnt>; while (<boolean expr>) { <body statements> <incr stmnt> }
A. Yes. If you use the continue statement in a loop block. In the for code, the counter will increase by one; in the while code, because it is skipped by continue, the counter will not increase by one.
Array
Q. Some Java developers use int a[] instead of int[] a to declare an array . What's the difference between the two?
A. Both usages are legal in Java, and their functions are the same. The former is the way to define an array in C. The latter is the method recommended by JAVA, because its writing method int[] better indicates that this is an array of int.
Q. Why do array subscripts start from 0 instead of 1?
A. This tradition originated from the programming method of machine language. In machine language, array subscripts are used to calculate the offset between an element's position and the first element. If you start from 1, you need to do a subtraction operation when calculating the offset, which is a waste.
Q. What happens if I use a negative number as an array subscript?
A. If the subscript is less than 0 or greater than or equal to the array length, JAVA will throw an ArrayIndexOutOfBoundsException exception when running and terminate the program.
Q. Are there any other pitfalls that need to be noted when using arrays?
A. You need to remember that JAVA will initialize an array when you create it, so declaring an array takes O(N) time.
Q. Since a[] is an array, why does System.out.println(a) print out a hexadecimal number, just like @f62373, instead of printing out the array? element?
A. Good question. This statement prints out the address of the array in memory. The system will automatically call the toString() method of the array. For this problem, you can look at the source code of the toString() method.
FunctionCall
Q. When using an array as a parameter when calling a function, I Often confused?
A. Yes. You need to remember the difference between pass-by-valueparameters (parameters are basic variable types) and pass-by-reference parameters (such as arrays).
Q. 那为什么不把所有的参数都使用传值的方式,包括对待数组?
A. 但数组很大时,复制数组需要大量的性能开销。因为这个原因,绝大多数变成语言支持把数组传入函数但不复制一个副本——MATLAB语言除外。
递归调用
Q. 有没有只能用循环而不能用递归的情况?
A. 不可能,所有的循环都可以用递归替代,虽然大多数情况下,递归需要额外的内存。
Q. 有没有只能用递归而不能用循环的情况?
A. 不可能,所有的递归调用都可以用循环来表示。比如你可以用while的方式来实现栈。
Q. 那我应该选择哪个,递归的方式 还是 循环的方式?
A. 根据代码的可读性和效率性之间做权衡。
Q. 我担心使用递归代码时的空间开销和重复计算(例如用递归解Fibonacci)的问题。有没有其他需要担心的?
A. 在递归代码中创建大数据类型(比如数组)时需要额外注意,随着递归的推进,内存使用将会迅速增加,由于内存使用增加,操作系统管理内存的时间开销也会增加。
排序与查找
Q. 为什么我们要花大篇幅来证明一个程序是正确的?
A. 为了防止错误的结果。二分查找就是一个例子。现在,你懂得了二分查找的原理,你就能把递归形式的二分查找改写成循环形式的二分查找。Knuth 教授在 1946年就发表了二分查找的论文,但是第一个正确的二分查找的程序在 1962年在出现。
Q. 在JAVA内建库中有没有排序和查找的函数?
A. 有的。在 java.util.Arrays 中包含了 Arrays.sort() 和 Arrays.binarySearch() 方法。对于Comparable 类型它使用了 归并排序,对于基本数据类型,它使用了快速排序。因为基本类型是值传递,快速排序比归并排序更快而且不需要额外的空间。
Q. 为什么JAVA库不用 随机pivot方式的快速排序?
A. 好问题。 因为某些程序员在调试代码时,可能需要确定性的代码实现。使用随机pivot违背了这个原则。
栈和队列
Q. 在Java库中有对stacks 和 queues 的实现吗?
A. Java库中内建 java.util.Stack,但是你应该避免使用它如果你需要一个真正的栈的话。因为它是实现了额外的功能,比如访问第N个元素。另外,它也支持从栈底部插入元素,所以它看上去更像是一个队列。尽管实现了这些额外的功能对编程人员是一个加分,可是我们使用数据结构并不只是想使用所有功能,而是需要我们正好需要的那种结构。JAVA对于栈的实现就是一个典型的宽接口的例子。
Q. 我想使用数组来表示一个包含泛型的栈,但是以下代码编译报错。为什么?
private Item[] a = new Item[max]; oldfirst = first;
A. 不错的尝试。不幸的是,创建一个泛型数组在 Java 1.5里不支持。你可以使用cast,比如下面的写法:
private Item[] a = (Item[]) new Object[max]; oldfirst = first;
根本的原因是JAVA中的数组是“协变的(covariant)”,但是泛型并不是。比如, String[] 是 Object[]的一种子类型,但是 Stackf7e83be87db5cd2d9a8a0b8117b38cd4并不是 Stacka87fdacec66f0909fc0757c19f2d2b1d 的一种子类型。 许多程序员认为“协变的”数组是JAVA在数据类型方面的一个缺点。但是,如果我们不考虑泛型,“协变的”数组是有用的,比如实现 Arrays.sort(Comparable[]) 方法,然后当参数是 String[]时它也可以被正常调用。
Q. 可不可以在数组上使用 foreach 方式?
A. 可以的(虽然 数组并没有实现 Iterator 接口)。请参考下面的代码:
public static void main(String[] args) { for (String s : args) StdOut.println(s); }
Q. 在 linked list 上使用 iterator 是不是比循环或者递归更有效率?
A. 编译器在翻译时,可能把那种“尾递归”形式翻译成等价的循环形式。所以可能并没有可以被观测到的性能提升。
尾部递归是一种编程技巧。如果在递归函数中,递归调用返回的结果总被直接返回,则称为尾部递归。尾递归是极其重要的,不用尾递归,函数的堆栈耗用难以估量,需要保存很多中间函数的堆栈。比如f(n, sum) = f(n-1) + value(n) + sum; 会保存n个函数调用堆栈,而使用尾递归f(n, sum) = f(n-1, sum+value(n)); 这样则只保留后一个函数堆栈即可,之前的可优化删去。
Q. 自动装箱机制会怎么处理下面的情况?
Integer a = null; int b = a;
A.它将返回一个运行时错误。基础类型不允许它对应的装箱类型里的值是null。
Q. 为什么第一组打印的是 true,但是后面两组打印的是 false?
Integer a1 = 100; Integer a2 = 100; System.out.println(a1 == a2); // true Integer b1 = new Integer(100); Integer b2 = new Integer(100); System.out.println(b1 == b2); // false Integer c1 = 150; Integer c2 = 150; System.out.println(c1 == c2); // false
A. 第二组代码打印 false 是因为 b1 和 b2 指向不同的 Integer 对象引用。第一组和第三组依赖于自动装箱机制。 令人意外的第一组打印了 true 是因为在 -128 和 127 之间的值会自动转换成同样的immutable型的Integer 对象。对于超出那个范围的数,Java会对于每一个数创建一个新的Integer对象。
The above is the detailed content of Summary of frequently asked questions for Java beginners (collection). For more information, please follow other related articles on the PHP Chinese website!