CS61B_值得注意的知识
1.Golden Rule of Equals For primitives, the line int y = x copies the bits inside the x box into the y box. For reference types, we do the exact same thing. In the line Walrus newWalrus = oldWalrus;, we copy the 64 bit address in the oldWalrus box into the newWalrus box. So we can think of this golden rule of equals (GroE) as: when we assign a value with equals, we are just copying the bits from one memory box to another!
等号黄金法则 对于原始类型,行int y = x将x盒子内的位复制到y盒子中。对于引用类型,我们做完全相同的事情。在行Walrus newWalrus = oldWalrus;中,我们将oldWalrus盒子中的64位地址复制到newWalrus盒子中。因此,我们可以认为这个等号黄金法则(GroE)是:当我们使用等号分配一个值时,我们只是从一个内存框复制位到另一个!
对于int double这种值来说,y = x 就是单纯的将x的bit值copy一份赋给y,当我们改变x的时候,y的值不会改变。
而对于一个类,譬如Walrus newWalrus = oldWalrus,我们则是将 oldwalrus 地址复制给 newwalrus ,此时就如同C++的传递指针,这个时候,如果改变newWalrus 也会改变oldWalrus.
Parameter Passing Say we have a method average(double a, double b). This method takes two doubles as parameters. Parameter passing also follows the GRoE, i.e. when we call this method and pass in two doubles, we copy the bits from those variables into the parameter variables.
2.数组的size 不能改变
the size of the array was specified when the array was created, and cannot be changed!
3.如果double 比int 更加精确,那么为什么不一直使用double 呢?
Doubles are more versatile than ints because they can represent fractional values with greater precision, as they have a larger range and a higher degree of precision. However, there are some potential disadvantages to using doubles over ints:
1. Memory usage: Doubles take up more memory than ints, so if you are working with large datasets or need to optimize memory usage, ints may be a better choice.
2. Performance: Since doubles require more memory and have more precision, they may be slower to process than ints, especially on older or lower-end hardware.
3. Comparisons: Due to the way that computers store floating-point values, comparing doubles for equality can be more complicated than comparing ints, and may lead to unexpected results.
4. Type safety: Because doubles are more permissive in terms of the range of values they can represent, it is easier to inadvertently introduce bugs or errors when using doubles, such as overflow or underflow issues.
In general, the decision to use ints or doubles (or another data type altogether) will depend on the specific requirements of your program or application, as well as the resources and constraints you are working with.
4.How much does the memory cost differ between the storing of an address of a 32 entry int array and a 300 entry int array?
The memory cost of storing the address of a 32 entry int array and a 300 entry int array is generally the same, regardless of the size of the array. This is because the address is typically represented using a fixed number of bits, which is determined by the computer's architecture and the size of its memory bus.
For example, on a 32-bit architecture, the address of an int array would be represented using 32 bits (4 bytes), regardless of whether the array has 32 or 300 entries. On a 64-bit architecture, the address would be represented using 64 bits (8 bytes).
However, the memory cost of storing the actual data in the array would be significantly different between a 32 entry int array and a 300 entry int array. Assuming each int requires 4 bytes of memory, a 32 entry int array would require 32 * 4 = 128 bytes of memory, while a 300 entry int array would require 300 * 4 = 1200 bytes of memory. Therefore, storing a 300 entry int array would require almost 10 times the memory of storing a 32 entry int array.
5.关于什么时候将嵌套类声明为static,以及声明为static的好处
If the nested class has no need to use any of the instance methods or variables of SLList, you may declare the nested class static, as follows. Declaring a nested class as static means that methods inside the static class can not access any of the members of the enclosing class.
This saves a bit of memory, because each IntNode no longer needs to keep track of how to access its enclosing SLList.