Java basics (two)-2019/12/19

Java basics (two)-2019/12/19

  • The difference between Array and ArrayList and their respective application scenarios
  1. The elements in Array can be basic data types or reference data types; the elements in ArrayList can only be reference data types.
  2. Array sizes are fixed, the ArrayList size is dynamic, ArrayList Based Array.
  3. Array is suitable for scenarios where the amount of data does not change, ArrayList is suitable for scenarios where the amount of data is uncertain or changes frequently
  • Value passing and reference passing in Java
  1. When the parameter is a basic data type, pass by value: pass a copy of the parameter, and modify the parameter within the function without affecting the value outside the function.
  2. When the parameter is a reference data type, pass by reference: Pass a copy of the object reference, you can modify the external object through the reference copy in the function, but you can point the reference copy to other objects. At this time, the reference copy is related to the original object Cut off.
  3. Note : When the parameter is String, although it is also passed by reference, because the String class is modified by the final keyword, any attempt to modify the string inside the function will create a new String object, and will not affect the String object outside the function.
  • Could you please explain why there is 4.0-3.6 = 0.40000001?

Simply put, it is because all numbers in the computer: integers and decimals are expressed in binary, and the use of binary can not accurately represent all decimal decimals.

  1. Take 0.3 as the column: 0.3 = 0.25 + 0.03125 + 0.015625 + …… = 2^-2 + 2^-5 + 2^-6 + ……, the sequence is infinite, but only limited digits can be used in the computer (32-bit or 64-bit) to represent a decimal, only 23 bits (float) or 52 (double) can be used to represent the decimal part, so the computer can only approximate the 0.3 decimal.
  2. Take 0.75 as the column: 0.75 = 0.5 + 0.25 = 2^-1 + 2^-2, the sequence is finite, so the computer can accurately represent the decimal number 0.75.
  • How are decimal numbers stored in a computer?

Decimal numbers are divided into unsigned numbers and signed numbers (only signed numbers are supported in Java). The storage of unsigned numbers is very simple. You only need to store the binary corresponding to the decimal number. Signed numbers are stored in two's complement . Briefly understand the representation of three signed numbers:

  1. Original code-the highest bit is the sign bit, and the remaining n-1 bits represent the absolute value of the value. For
    example, in a computer with a word length of 4:
    Value Representation
    0 0000 \ 1000
    1 0001
    -1 1001
    2 0010
    -2 1010
    It can be observed that there are two representations of 0, and -1 + 1 = 1010 = -2. This obviously has a numerical calculation error, so the original code cannot be used to represent a signed number.

  2. Inverse code-the highest bit is the sign bit, the inverse code of a positive number is the same as the original code, and the inverse code of a negative number is the bit-inverse of its absolute value. From this, the weight of the highest bit of the inverse code is -(2^( n-1)-1) For
    example, in a computer with a word length of 4:
    Value Representation
    0 0000 \ 1111
    1 0001
    -1 1000
    2 0010
    -2 1101
    It can be observed that there are two representations of 0, and -1 + 1 = 1001 = -6, which is obviously a numerical error. Therefore, one's complement cannot be used to represent a signed number.

  3. Complement code-the highest bit is the sign bit, the complement of a positive number is the same as the original code, and the complement of a negative number is the absolute value of the inverse of the absolute value plus 1, from this it can be concluded that the weight of the highest bit of the complement is -(2 ^(n-1)) For
    example, in a computer with a word length of 4:
    Value Representation
    0 0000
    1 0001
    -1 1111
    2 0010
    -2 1110
    It can be observed that there is only one representation for 0, and various combination operations produce correct results. The proof is skipped.
  • == What does it compare?

If "==" compares reference data types, it compares whether the reference points to the same object; if it compares basic data types, it compares whether the values ​​are equal.

  • Explain why hashCode() is rewritten when rewriting equals()?

When using containers such as HashMap to store objects, it is necessary to combine hashCode() and equals() to determine whether an object already exists in the container. First call hashCode() to calculate the location where the object should be stored, and then call equals() to determine whether the object already exists in the container. If hashCode() is not rewritten, then two objects judged to be equal by equals() are likely to have the same hashCode (hashCode is the memory address by default), so they will be stored in different locations of the HashMap, which is obviously illogical.

Guess you like

Origin blog.csdn.net/a16302010048/article/details/103653911