I have to. 10 Points Java Basics

Source: Internet
Author: User
Tags instance method modifier

1. Instance variables and class variables

Instance variable: refers to each object independent, modifies one of the object's instance variable, does not affect the other instance variable's value, the variable value does not have the static keyword adornment;

Class variables: Refers to all objects shared, one of which modifies the value of the variable, then all objects of the variable value has been modified, using the static keyword decoration, generally use the "class name. Attribute" to access;

2. Static and non-static methods

Static methods: Refers to methods that are decorated with the static keyword, typically accessed using the class name. method, or accessed through the object. method, but not recommended! Static methods can only invoke static methods and use static members, because static members and static methods are loaded as the class loads, non-static members or methods are loaded later than static members and methods, so static methods cannot invoke non-static methods and non-static members, that is, static to static;

Non-static method: Refers to a method that is not decorated with the static keyword, and is generally accessed using the object. method, which can be used to invoke a non-static method in a non-static method and use a non-static member to invoke a static method and use a static member;

the role of the package in 3.Java

In Java, the declaration of a package is generally written in the first line of the file , using the keyword Package + packet name, which has the effect of: ① avoid duplicate names, for example, we sometimes call a method of a class to find the same class name method, At this point you have to import the package of the class you want to call, or use the full class name to call, so as to avoid duplicate names; ② is used for permission control, classes under the same package can be called each other, and if the class under the other package is called, it is necessary to import the package containing the calling class, thus avoiding illegal external calls In addition, it is reasonable to use the access control permissions of the keyword public , protected, private to access the settings (more information on the control of the permissions of the reference Schedule 1); ③ divides the project hierarchy so that file management is more organized, such as MVC, MVP mode subcontracting;

Schedule 1static import in 4.Java

Static import is a new feature introduced by JDK1.5, and in general, we call static members or static methods in a class using the class name. property to invoke, whereas static imports can import static members (methods and variables) under a class into a class-like approach, that is, you can import directly to the member level of the classes ( Methods and variables) so that the static and static methods of the imported class are directly visible in the current class, and using these static members without using the "class name. Attribute" method to invoke the methods name or property directly, as simple as calling methods and properties in your own class.

The syntax for static import is:

    • Import static package name. class name. static member variable;
    • Import static package name. class name. static member function;

Note that you are importing static member variables and method names, and you can also import all static members under a class:

    • Import static package name. class name. *;

The * number represents the full distribution.

In general, we can moderate the use of static imports, not recommended a lot of abuse, because we know that Java is an object-oriented language, the use of static import calls, the absence of the class name, that is, to weaken the description of the class, sometimes we do not know which class to call the members, may also think about, weaken the object-oriented thinking Causes the call relationship between classes and classes to be unclear!

the This keyword in 5.Java

In Java, when an object is created, the Java Virtual machine assigns it a pointer to the object itself, which is * * this**. This usage scenario: ① When a method's layout variable and the name of the instance variable are the same, you need to use the ThisKeywords to differentiate, if not the same name can also be used, usually do not have the name of the case does not write This;② useThis . Method name (parameter);To invoke the member method of the class, normally without writing this, calling directly; ③ usingThis (parameter 1 ...);To invoke other construction methods in this class, such as customizing a View in Android, typically writes 3 overloaded construction methods, which are used in the construction of a single parameterThis (parameter 1, parameter 2);To invoke the two-parameter construction method, the construction method called three parameters in the two-parameter constructor method; ④this is passed as a parameter, and in the Activity of Android, this is often used as an argument, and ⑤ the method of calling an external class in an inner class or an anonymous inner class. If you use this in an inner class, this represents the current class itself, which is the inner class, and the method you want to invoke in the external class must use theexternal class name. This. Method name ();to invoke;

Also use this keyword to pay attention to several points:

  • Using this to invoke another construct in the constructor method, you must place the call in the first sentence of the method and use it only once;
  • You cannot use this in an instance method to invoke a construct, because the invocation of an instance method must have an object, and the creation of the object is bound to call the constructor method, and the call to the constructor method should be earlier than the instance method;
  • This can not be used in construction to invoke the construction of each other, for example, if you invoke the construction of a single parameter in the construction of a single parameter, in the construction of a two-parameter construct, we use the construction of a single parameter to create an instance, the invocation process is: single-parameter construction, double-parameter construction, single parameter construction, This will be an endless invocation, and this violates the rules of constructing methods that can only call one parameter at a time in a method's call chain;
Super keyword in 6.Java
  • super (); The calling parent class is the parameterless construct of object, which is automatically called by default and is omitted in general;
  • This and super keywords are to solve the same name problem, if there is no duplicate the problem can be omitted to write;
  • This and super can all be understood as a reference to the parent class, that is, the properties and methods in the parent class can be called;
  • Super. Method Name (); Represents the method that invokes the parent class;
  • Super. Property indicates that a property in the parent class is called (Not many);
  • super (); Call the constructor method of the parent class;
  • This and super can only be placed on the first line of the method, so they cannot appear together;
  • Super can only appear in the construction method;

In Java, the parent class of all classes is object, and if a class does not inherit the parent class by using the extends keyword, it inherits the object class by default, and the subclass does not inherit the constructor of the parent class, but must call the parent class's constructor, if not called. Must be manually to call, or will compile error;

overloading and rewriting in 7.Java

Overloading is overload, in the same class, the method name of the same parameter list of different methods are called overloaded methods;

Override is override, in the child parent class, the child class is the same as the parent class method name, the argument list must be the same, for the return value type subclass is either consistent with the parent class, or the parent class returns a subclass of the value type, and for the permission modifier, the child class's permission modifier is either the same as the parent class, or more If an exception is thrown, the subclass's exception capture scope is either the same as the parent class or the exception scope of the parent class is smaller, that is, the subclass exception is the subclass of the parent class exception;

8.== and equals

The relational operator "= =" generates a Boolean result that calculates the relationship between the operands ' values, and if it is the base data type, it compares their value itself, and if it is a reference data type, compares the address of the object in memory;

The Equals method is a method in the base class object, and all classes inherit directly or indirectly from the object class, so there is a method in all classes, and if you do not override the equals () method, call the equals () method and use the = = the same effect, is also the comparison of in-memory address values, in most Java-provided classes, such as String,double,date,integer, are overridden by the Equals method to compare whether the object to which the objects are stored is equal. If it is our own defined class, if you do not override the Equals method, using equals by default compares the object's in-memory address, to compare the content, you must override the Equals method.

9. Understanding of the class

Class is an abstract description of a class of things, objects are examples of classes, can be understood as a class is a car design, the object is a real production of cars, a design diagram (class) can produce more than one car (object), the class contains properties and methods, attributes describe the characteristics of the class, the method describes the specific functions of the class, In this way, when we design the class, we will pay more attention to the functions and features of a class, abstract them into the class, external calls do not need to care about the specific implementation of the class, so you can more consider the relationship between classes and classes, so that the benefit is to improve the reuse of objects, reduce the development difficulty, Much of Java is object-oriented programming, everything is object. For object-oriented development, it is generally divided into three steps: OOA (object-oriented analysis), OOD (object-oriented design), OOP (object-oriented programming). OOA that is, according to the specific function of the analysis of what characteristics or methods, OOD is based on the characteristics and methods of abstract design specific classes;

10. Variables, memory and garbage

A variable is a storage area in memory, and its value is changed, not fixed;

The memory in Java is generally divided into Java virtual machine stack and heap memory, the stack is generally stored in the basic data types and reference types of reference variables, heap memory is stored in the object instance, Java heap is the Java Virtual Machine management of the largest piece of memory, it is shared by all threads of memory area, When the virtual machine is started, it can not be a contiguous area of physics, as long as it is logically contiguous, and the concept of the memory allocation area such as method area, static constant pool, program counter, local method stack, etc.

Garbage: Refers to the object (a heap of memory space) that no reference points to, and this space is garbage, and all garbage waits for the GC (garbage collector) to recycle (free memory space) periodically.

More Dry articles follow my public number:

Write a picture description here

I have to. 10 Points Java Basics

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.