Java SE Basics (ii)

Source: Internet
Author: User
Tags java se

1. The class consists of two parts: Properties and methods. Attributes are generally represented by nouns, and methods are generally expressed by verbs.


2. if more than one class is defined in a Java source file, only one of these classes can be public and not public.


3. When a method parameter is passed in Java, regardless of whether the native data type or reference data type is passed, the parameter is passed uniformly as a pass by value, and there is no concept of passing the reference (pass by reference) in Java.


4. method Overloading (overload): Indicates that two or more methods have the same name, but the method parameters are different. There are two levels of different method parameters:

1). Different number of parameters

2). Parameter return value is different


5. If you want to invoke another constructor method in one of the constructor methods, you can call it by using this (), which represents the parameters in the target constructor method. This () must be the first statement of the constructor method.


6. Java is single-inheritance


7. Super Keyword: Super represents a reference to the parent class object.


8. If a subclass uses super () to display a constructor method that calls the parent class, it will look for the constructor that corresponds to super () when executing, without looking for the parent class's constructor without parameters.

Like this, Super() must also be executed as the first statement of the construction method.


9. when the parent class does not have a constructor method with no arguments, the subclass needs to use super to display the constructor of the parent class, and Super refers to a reference to the parent class.


method Override (override): Also called overwrite, the subclass is the same as the method return type of the parent class, the method name is the same as the parameter, so we say that the subclass and the method of the parent class constitute an overriding relationship.


one . when two methods form an overriding relationship, the run () method of the parent class can be called through Super.run () in the subclass method, where Super.run () does not have to be placed in the first line of the statement, Because the parent class object is now constructed,

The Run method of the parent class is called first or the Run method of the subclass is called first, as determined by the program logic.


when defining a class, if the class's parent is not explicitly specified, then the class inherits from the Java.lang.Object class (a class provided by the JDK, the Object class is the direct or indirect parent of all classes in Java).


Polymorphism (polymorphism): So-called polymorphism, a reference to a parent type can point to an object of a subtype, or a reference to an interface type can point to an instance of the class that implements the interface.


Force type conversions:

1). Up-type conversion (UPCAST): For example, convert the Cat type to the Animal type and convert the subtype to the parent type. For up-type conversions, you do not need to specify explicitly.

2). Down type conversion (downcast): For example, convert the Animal type to Cat type. Converts the parent type to a subtype. For down-type conversions, you must explicitly specify (you must use coercion type conversions).


Abstract class: A class that uses the abstract modifier is called an abstract class, and an abstract class cannot be instantiated.


If a class contains an abstract method, then the class must be an abstract class. However, abstract classes can also contain specific methods (declarations, implementations).


in case the subclass inherits the parent class (the parent class is an abstract class), then the subclass must implement all the abstract methods defined in the parent class, otherwise the subclass needs to declare an abstract class.


Interface Description:

1). All methods in the interface are abstract methods. When you declare a method in an interface, you can use the abstract keyword or you can not use it. Usually omitted.

2). A class implements an interface, and the class must implement all the methods declared in the interface. If the class is an abstract class, you can not implement the methods in the interface.

3). You can also define member variables in an interface. The member variables in the interface are public, final, static.


The . Static Keyword:

1). Static Modifier property: Regardless of how many objects a class generates, all of these objects use a unique static member variable, and one object modifies the static member variable, and the value of that static member variable of the other object

Change with it. If a member variable is static, then we can pass the class name. The name of the member variable to use it (recommended in this way).

2). Static Modification Method: The method of static modification is called a statics method. For static methods, you can use the class name. Method name to access it in the same way.

3). static methods can only inherit and cannot be overridden.


. Static code block:

1). Static code block. The function of static code blocks is also to do some initialization work. Execute the static code block first, and then execute the construction method. Static blocks of code are executed when the class is loaded, and the construction method is executed when the object is generated;

To invoke a class to generate an object, you first need to load the class onto the Java virtual machine (JVM) and then the JVM loads the class to generate the object.

2). The static code block of the class executes only once, when the class is loaded, because each class is only loaded once, so the static block is only executed once, and the constructor does not, and the class is called every time an object is generated .

constructor method, so the new one invokes the constructor method once.

3). If the inheritance system has both a constructor and a static code block, execute the static code block of the topmost class first, execute the static block of code to the lowest class, and then execute the constructor of the topmost class and execute it to the lowest class

Method of construction. Note: Static code blocks are executed only once.


Final keyword:

1). Final modifier class: When a class is modified by final, it means that the class is an end-state class, which cannot be inherited.

2). Final modification method: When a method is modified by final, it means that the method is an end-state method, that is, it cannot be overridden (override).

3). Final Modifier property: Indicates that the property cannot be overwritten when a property is modified by final. When final modifies a native data type, the value that indicates that the native data type cannot change from (for example, cannot be changed from 10);

If final modifies a reference type, the indicates that the reference type can no longer point to another object, but the contents of the object pointed to by the reference may vary.


The number of final type member variables is assigned as follows:

1). Assign an initial value when declaring a member variable of the final type

2). The initial value is not assigned when declaring a member variable of the final type, but it is assigned an initial value in all the constructor methods of the class.


static access is static only, and non-static access to everything. The This keyword cannot be used in a static method.


Package: Used to classify classes that complete different functions, and put them under different directories (packages). Naming rules for packages: reverse the company domain name as the package name. for Package name: each letter needs lowercase. If the package is not used when defining the class,

Then Java would assume that the class we define is in the default package.


There are two ways to compile a Java source file with the package declaration:

1). Compile directly and then manually build the directory structure based on the package name defined in the class, and finally put the generated class file into the directory structure (rarely used, more cumbersome).

2). Use the compile parameter –d, in the javac–d mode. source files. Java, so that after compiling, the compiler will automatically help us build the directory structure of the package.


Import a.bb.*, which indicates that all classes under the A.BB package are imported. Import aa.bb.* does not import the class under the aa.bb.cc package.


If the two classes are under the same package, they do not need to be imported and can be used directly.


access modifier (access modifier):

1). Public: The properties and methods that are modified by publicly can be accessed by all classes.

2). protected (Protected): properties and methods that are modified by protected can be accessed within the class, the same package, and the subclasses of the class.

3) Private (private): properties and methods that are modified by private can only be used inside that class

4). Default (no access modifier): used within the class and the class under the same package.


instanceof: Determines whether an object is an instance of a class. Syntax form: Reference name instanceof class name (interface name), returns a Boolean value. The return value is also true when the class name is the parent class name.


Comparison of equality (= =)

1). For native data types, the comparison is between the left and right sides of the value is equal.

2). For reference types, compare whether the left and right references point to the same object, or whether the reference addresses are the same on both sides.


The class under the Java.lang package does not need to display the import, and the compiler will automatically import it for us at compile time.


16 Binary, every 16 in one, 16 binary numbers include: 0~9,a,b,c,d,e,f, letters are not case sensitive.


The Equals () method, which is defined in the object class, so that every class in Java has that method, and for the Equals () method of the object class, it is a method that determines the invocation of the Equals () method. Whether the reference is consistent with the reference passed in,

That is, whether the two references point to the same object. For the Equals () method of the Object class, it is equivalent to = =


When you print a reference, you actually print the return value of the toString () method that refers to the object being referenced, because each class is directly or indirectly inherited from object, and ToString () is defined in the object class. So each class has the ToString () method.

The main content of this article is from Santhiya Garden notes.

Java SE Basics (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.