Java basic learning day45 (package, final, constant)

1. What is a package

  • A package is a folder, which is used to manage java classes with various functions, which is convenient for later code maintenance
  • The rules of the package name: the company domain name is reversed + the function of the package, all English lowercase is required, see the name to know the meaning. com.nuist.domain
    package com.nuist.domain;
    //The path of the current class of the table name is defined in the com.nuist.domain package, and the idea is automatically generated

2. When do you need to import the package? When is the import package not needed?

  • When using classes in the same package, there is no need to import the package
  • When using classes in the java.lang package, there is no need to import the package
  • In other cases, a guide package is required.
    import com.nuist.domain.Student;
    //Indicates that all the Student classes in this class are in the package com.niust.domain
  • If you use classes with the same name in two packages at the same time, you need to use the full class name
  • Full class name/fully qualified name: package name + class name com.nuist.domain.Student

3. final

  • final modified method: Indicates that the method is the final method and cannot be overridden. If the current method is a rule and you don't want others to change it, then this method can be modified with final.
  • Final modified class: Indicates that the class is a final class and cannot be inherited. If you do not want others to rewrite all the methods in the current class, then this class can be modified with final
  • Final modified variable: Indicates that the variable is a constant and must be assigned a value when it is defined, and can only be assigned once, and cannot be changed.

4. Constants

  • In actual development, constants are generally used as system configuration information to facilitate maintenance and improve readability
  • Naming conventions for constants:
    a. Single word: all uppercase
    b. Multiple words: all uppercase, separated by underscores
  • Details:
    a. The final modified variable is a basic type: the data value stored in the variable cannot be changed b
    . The final modified variable is a reference type: the address value stored in the variable cannot be changed, but the space pointed to by the address and the content in the space Can be changed
    c. Core: The data recorded by the constant cannot be changed

For example: the string cannot be changed
Reason: the content of the string is stored in a byte type array named value, this array is modified by final, so the address value recorded by value cannot be changed, and this array is still modified by private Yes, the string class does not provide get() and set() methods, so the outside world cannot obtain the address value of the value record, and cannot modify the content pointed to by this address. In summary, the string cannot be changed.

おすすめ

転載: blog.csdn.net/u011453680/article/details/129186758