Java Notes

Java Notes

  • Each java source file can only have one class of public type , and this class must be the same as the file name.

  • Instance variables always have default values, even if they are not explicitly assigned.

默认值:

int     0
float   0.0
boolean false
引用      null
  • ==For primitive main data types, it is a byte comparison, and for reference types, it is to compare whether the same object is referenced .

  • For cross-type conversion in java (such as from String to int), you need to call built-in classes to complete the conversion. for example,Integer.parseInt("3")

  • When the for-in loop traverses, only the copy is called

  • Java's random number is provided by the random() method of the Math class, which produces a decimal between 0 and 1.

  • Similarly (int), it becomes the cast operator, which is a forced type conversion.

  • About abstract classes and abstract methods

    • Java supports (run-time polymorphism) natively, while C++ must use the virtualkeyword to decorate the function to achieve.

    • Pure virtual functions in C++ are equivalent to abstract methods in Java.

    Conjecture: Because Java enforces object orientation, there will be no global functions, so there may be no need to distinguish between global functions and member functions as in C++. Similarly pure virtual functions and function declarations may have a similar relationship.

    Conjecture: All members of non-abstract classes will be "constructed into entities", so non-abstract classes cannot have abstract methods. Java's behavior of using abstract classes to constrain abstract methods may be better than C++'s constraining constraints.

  • About Inheritance and Polymorphism

    • Java does not allow multiple inheritance, so a class can only inherit (extends) a normal class. But it can inherit multiple interfaces at the same time.
    • A reference declared by java as the parent class can be assigned to the entity of the child class, but only the methods of the parent class can be called.
  • abstract classes and interfaces

    • Abstract classes allow non-abstract functions, that is, functions that are actually defined.
    • Interfaces do not allow non-abstract functions.
    • Subclasses can inherit only one abstract class, but can inherit multiple interfaces.
    • An interface must be implemented, and an abstract class can be inherited by an abstract class.
    • The concept of class inheritance is vertical, while the concept of interface implementation is parallel. The husky class inherits the dog class, the dog class inherits the animal class, and the husky must be an animal.

    Therefore, there will be no diamond inheritance when using interfaces, because any interface must have no parent class (interface)

    • And because the interface must be implemented, there cannot be a parent interface, so it can't be stacked at all, and there will be no vertical. Therefore, if the Husky class is completed with an interface, it will implement both the dog interface and the animal interface. But this is unreasonable, because dogs are animals, and they are naturally stacked. So, it's reasonable for the Husky class to implement the Second and Howl interfaces.

    It can be seen that although classes and interfaces are similar, they are not the same thing at all. The class is definite and consistent, just like the classification of goods in the mall , while the interface is flexible and descriptive, like the label of the goods

  • About member access

    • In java, subclasses cannot reduce the permissions of parent class member methods, but can increase them (such as protected becomes public).
    • The permissions of member variables cannot be modified.

    Because it is assumed that an interface defines a set of rules to ensure that any subclass that implements this interface can have this method callable, but it will be embarrassing if the subclass is changed to private

  • About static and non-static methods

    • Static methods belong to a class, not a separate object.
    • Non-static methods belong to each object, not to a class.

    In fact, in a sense, a static method is a function in procedural programming, that is, it wants a specific function, and does not belong to any objective relationship. Even if it belongs to a certain class, it is only a simple classification, and there is no logical stacking relationship, that is to say, the class name before the static method acts as a namespace in a certain sense.

    • Non-static methods can call static methods, while static methods cannot (directly) call non-static methods (either inside or outside the class).
    • Non-static methods in this class can call non-static methods, but cannot (directly) call non-static methods of other classes.
    • Static methods can call each other at any time.

    In fact, the above problem is an instantiation problem: the non-static method is called anywhere except in the non-static method of this class, and the class needs to be instantiated as an object before being called.

  • About method calls

There is an interesting comparison:

代码1public class ClassTest {
    public static void main(String[] args){
        A a = new B();
        a.get();
    }
}

class A{
    int a = 0;

    public void get(){
        System.out.println(a);
    }
}

class B extends A{
    int a = 1;
}

输出: 0

代码2public class ClassTest {
    public static void main(String[] args){
        A a = new B();
        a.get();
    }
}

class A{
    int a = 0;

    public void get(){
        System.out.println(a);
    }
}

class B extends A{
    int a = 1;

    public void get(){
        System.out.println(a);
    }
}

输出: 1

This illustrates 2 (actually obvious) problems:

  1. When using the parent class's reference variable to manipulate the subclass's object, the method overrides the subclass's version first (logically).
  2. The first example also shows that instance variables are only scoped within the class. Therefore, the methods of this class can only access the variables of this class (without any additional conditions).

    • Definition of constants

    • My teacher said that a variable modified with static is a constant, but it is not, because it can be overridden by subclasses and can also be modified by static methods.

    • Only variables modified with final and static modifiers can be used as constants.

    • About Overriding and Overloading

    • Overriding refers to overriding a new method that is exactly the same as the method form (return value and parameters and function name) in the parent class (or implemented interface).

    • Overloading is writing a new method with the same name as an existing method but with a different parameter list.

So the following code is neither an override nor an overload, and will not compile at all

class A{
    int a(){
        ...
    }
}

class B extends A{
    double a(){
        ...
    }
}

Continuing to update…

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326832773&siteId=291194637