Like Java data types, Java operators, Java flow control, Java object oriented, Java exception handling, these are the most basic things, is the beginning of learning Java must master the knowledge point. — Paying attention to Lebyte takes you into the world of Java.
Java Overview:
In 1991, Sun’s James Gosling and others began to develop a language called Oak, hoping to control microprocessors embedded in cable TV switch boxes, PDAs, etc.
In 1994, the Oak language was renamed Java;
There are three technical architectures for Java:
JAVAEE: Java Platform Enterprise Edition, developing applications in the Enterprise environment, mainly for Web application development;
JAVASE: Java Platform Standard Edition, which implements desktop application development and is the foundation of the other two;
JAVAME: Java Platform Micro Edition, which develops applications in consumer electronics and embedded devices such as mobile phones;
1, JDK: Java Development Kit, Java Development and runtime environment, Java Development tools and JRE.
Java Runtime Environment (JRE) : Java Runtime Environment (JRE) : Java Runtime Environment (JRE) : Java Runtime Environment (JRE)
3, configure environment variables: let the Java JDK \bin directory tool, can run in any directory, the reason is, the tool directory told the system, when using the tool, by the system to help us to find the specified directory.
Configuration of environment variables:
1) : permanent configuration: JAVA_HOME=% installation path %\Java\ JDK path=% \ JAVA_HOME%\bin
2) : temporary configuration: set path=%path%; C:\Program Files\Java\jdk\bin
Features: By default, the system searches for the program to be executed in the current path. If the program does not exist, the system searches for the program in the specified path.
Classpath configuration:
1) : permanent configuration mode: classpath=. c:; e:
2) : set classpath=.; c:; e:\
Note: Something to be aware of when defining the CLASSPath environment variable
If the environment variable classpath is not defined, Java starts the JVM and looks for the class file to run in the current directory.
If classpath is specified, the class file to run will be looked up in the specified directory.
Do you still look in the current directory? There are two cases:
1) : If the value of classpath ends with a semicolon and no running class is found in the specific path, the current directory will be searched again by default.
2) : If the value of classpath results in no semicolons, the running class will not be found in the current directory.
Generally, the semicolon is not specified. If the class file to run is not found in the specified directory, an error is reported, so that the program can be debuggable.
What do javac commands and Java commands do?
Remember that Java has two parts: one is to compile and one is to run.
Javac: Is responsible for compilation. When javac is executed, the Java compiler program is launched. Compiles a. Java file with the specified extension. Bytecode files that the JVM can recognize are generated. This is the class file, which is the Java runtime.
Java: The part responsible for running. The JVM is started. The libraries required by the runtime are loaded and executed against the class file.
For a file to be executed, there must be a starting point for execution, and that starting point is the main function.
Ii. Java Syntax basics:
1. Keywords: in fact, words given special meanings by a certain language.
Reserved words: words that have not yet been given a special meaning but are ready to be used in the future.
2. Identifiers: in fact, they are self-defined nouns in the program. Class names, variable names, function names. The value can contain 0 to 9, a to z, $, and _.
Note:
1), numbers cannot begin.
2), do not use keywords.
3. Constant: data that does not change in the program.
4. Variable: A storage space in memory used to store constant data.
Function: convenient for operation. Because some of the data is uncertain. So determine the name and storage space for the data.
Features: Variable space can be reused.
When are variables defined? Variables are defined whenever data is uncertain.
What does it take to create a variable space? * * *
1. What data should be stored in this space? Data type.
2, what is the name of this space? Variable name.
3. What is the first entry in this space? The initialization value of a variable.
Scope and lifetime of variables:
Scope of variables:
The scope starts at the location of the variable definition and ends at the pair of braces in which the variable resides.
Life cycle:
A variable is alive in memory from its defined location;
A variable is lost in memory when it reaches its scope;
Data type:
1) : basic data types: byte, short, int, long, float, double, char, Boolean ** 2) : reference data types: array, class, interface.
Automatic type conversion: From a lower level to a higher level, the system automatically converts.
Casts: When are they used? Assign a higher-level number to a lower-level variable of that number;
Operation symbol:
1) Arithmetic operators.
-
-
- / % %: Any integer module 2 is either 0 or 1, so as long as the moduli can be changed to achieve switching operation.
-
+: indicates the concatenation.
+ +, –
2) Assignment operator.
= += -= *= /= %=
3) Compare operators.
Property: The property of this operator is that the result of the operation can be either true or false.
4) Logical operators.
& | ^ ! && ||
Logical operators except! All are used to join two Boolean expressions. &: The result is true if both sides are true. Otherwise false.
| : as long as both sides to false result is false, otherwise it is true
^: different from or slightly different from.
If it’s the same, it’s false.
If the result is different, it is true.
The difference between & and && : & : Whatever the left side results in, the right side is involved in the operation.
&& : short circuit and, if the left side is false, then the right side does not argument and operation.
| and | | difference: | : operation on both sides.
| | : short circuit or, if the left is true, then the right not to participate in the operation.
5) Bitwise operators: operators used to operate on binary bits.
& | ^
& << >> >>>(unsigned right shift)
5. statement.
If switch do while while for
When are these statements used?
1) When checking a fixed number of values, you can use if or switch.
However, switch is recommended for high efficiency.
The switch (variable) {
Case value: the statement to execute; break;
…
Default: the statement to be executed.
}
How it works: Compare the value of the variable in parentheses with the value after the case, and which value after the same case
Execute the statement following either case or default if none is the same.
Details: A: Break can be omitted. If omitted, the execution will continue until a break is encountered.
B: The variable in the parentheses after switch should be one of the four types: byte,char,short, and int.
C: Default can be written anywhere in the switch structure. If the default statement is placed on the first line, the program executes from default until the first break occurs, regardless of whether expression matches the value in the case.
2) If is used when determining the range of data and obtaining the Boolean type.
3) Use a loop structure when certain statements need to be executed many times. While and for are interchangeable.
The difference is: if you need to define variables to control the number of cycles. Use for instead. Because the for loop ends, the variable is freed in memory.
Break: Used on switches and loop statements to break out, or terminate.
If the break statement exists alone, do not define other statements below, because compilation will fail if it cannot be executed. When loops are nested, break only jumps out of the current loop. To break out of the nested outer loop, you simply give the loop a name, called a label.
Continue: used only on loop structures to continue the loop.
Function: End this cycle, continue the next cycle. If the statement exists alone, the following statements cannot be defined and cannot be executed.
6. Functions: To improve the reuse of code, it can be defined as a separate function, which is embodied in Java functions. Functions are one example.
Functions in Java are defined in the following format:
Return value type function name (parameter type form parameter 1, parameter type form parameter 1…) {
Execute statement;
Return Return value;
} If there is no specific return value, the return value type is represented by the void keyword.
If the return type of a function is void, the return statement can be omitted and will be automatically added for you.
Return terminates a function. End The function.
How do you define a function?
A function is a function. Defining a function is to implement a function.
1) To specify the result of the operation of this function is to specify the return value type of this function.
2) In the process of realizing this function, whether there is unknown content involved in the operation is actually to clarify the parameter list of this function (parameter type & parameter number).
The function does:
1) Used to define functions.
2) Used to encapsulate code to improve code reuse.
Note: you can only call a function from a function, not define it.
Main function:
1) Ensure the independent operation of the class.
2) Because it is the entry point to the program.
3) because it is being called by the JVM.
What is the name of the function definition?
Answer: 1) In order to mark the function and make it easy to call.
2) In order to make the function clear by name, in order to increase the code reading.
Overloading is defined as: if two or more functions with the same name appear in a class, the function is overloaded as long as the number of arguments or the types of arguments are different.
How to distinguish overloading: When a function has the same name, just look at the argument list. It doesn’t matter what the return value type is.
Number group: a container used to store the same type of data. Benefits: Data in this container can be numbered, starting at 0. An array is used to encapsulate data, which is a concrete entity.
How do you represent an array in Java? Two forms of expression.
1) element type [] variable name = new element type [number of elements];
2), element type [] = {element 1, element 2… };
Element type [] name = new element type []{element 1, element 2… };
// Binary search. There must be a prerequisite: the elements in the array must be ordered.
Java is divided into five pieces of memory.
1: register. 2: local method area. 3: Method area. 4: stack. 5: the heap.
Stack: Stores local variables (variables defined in a function, parameters in a function, variables in a statement);
The data is released as soon as the region where the operation was completed ends.
Heap: Used to store arrays and objects, namely entities. What is an entity? Is used to encapsulate multiple data.
1: Each entity has a memory header value.
2: Variables in heap memory have default initialization values. Because the data type is different, the value is different.
3: Garbage collection mechanism.
Three: object oriented: ★★★★★
Characteristics: 1: Simplify complex things.
2. Object orientation turns the former executor of the process into the leader.
3: The idea of object oriented is in line with people’s thinking habits of an idea.
How are procedures and objects represented in our programs? A process is a function; An object encapsulates something like a function.
Anonymous object usage scenarios:
1: Anonymous objects can be used when a method is called only once.
2: Anonymous objects cannot be used when objects make multiple calls to members. You must give the object a name.
Definitions in a class are actually called members. There are two kinds of members:
1: Member variables: in fact, they correspond to the attributes of things.
2: Member function: actually corresponding to the behavior of things.
So, when you define a class, you define member variables and member functions. However, before definition, attributes and behaviors of things must be analyzed before they can be reflected by code.
private int age; // Private access is the lowest, and only access in this class is valid.
Note: Private is just one form of encapsulation.
Private members: Other classes cannot directly create object access. Therefore, private members can be accessed only by providing specific access methods to the class.
Benefits: You can add logical judgment and other operations in the function to judge the data and other operations.
Summary: When developing, keep in mind that attributes are used to store data and can be accessed directly, which is a security risk, so attributes in a class are often privatized and provided with public access methods.
This method usually has two canonical forms: for the property XXX, you can use setXXX() and getXXX() to operate on it.
Why is there no main function defined in the class?
Note: The main function exists only if the class needs to be run independently. If not, the main function is not defined.
The main function is guaranteed to run independently of the class, is the entry of the program, called by the JVM.
Differences between member variables and local variables:
1: Member variables are defined directly in the class.
Local variables are defined in methods, parameters, and statements.
2: Member variables are valid in this class.
A local variable is valid only within its own brace. The brace ends and the local variable is out of scope.
3: Member variables exist in heap memory, as the object is created, disappear and disappear.
Local variables exist in stack memory and are released when the region to which they belong runs.
Constructor: used to initialize an object, is given to the corresponding object to initialize, it has specific, one of the functions.
Features:
1: The name of this function is the same as the name of its class.
2: Return value types do not need to be defined.
3: This function has no concrete return value.
Remember: all objects need to be initialized before they can be used when they are created.
Note: When a class is defined, if the constructor has not been defined, the class will automatically generate a null parameter constructor. To facilitate the class to create objects, complete initialization. If you define a constructor in a class, the default constructor is gone.
There can be multiple constructors in a class, which can only be distinguished by argument lists because they all have the same function name. So, if there are multiple constructors in a class. They exist in terms of overloading.
What’s the difference between a constructor and a normal function?
1: The two functions are defined in different formats.
2: The constructor is called when the object is created for initialization, and the initialization action is performed only once.
General functions, after the object is created, need to be called before execution, can be called multiple times.
When to use constructors?
When you analyze something and find that it has some characteristics as soon as it appears, you define those characteristics in the constructor.
What is the difference between a constructor block and a constructor?
Construct code block: is to initialize all objects, that is, all objects will call a code block, as soon as the object is created, this code block will be called.
Constructor: initializes the corresponding object. It is specific.
“Person p = new Person();”
What does creating an object do in memory?
1: First load the person. class file in the specified location on the hard disk into memory.
2: When the main method is executed, space for the main method is opened in the stack memory (push-push), and then a variable p is allocated in the stack area of the main method.
3: create a physical space in heap memory, allocate a memory header value. new
4: Space allocation of attributes in the entity space and default initialization.
5: Initializes the display of attributes in the space.
6: Initializes the entity construction code block.
7: Initializes the constructor by calling the corresponding constructor of the entity. (a)
8: Assign the first address to p, and the p variable references the entity. (Pointing to the object)
Encapsulation (one of the object-oriented characteristics) : Hides the attributes and implementation details of an object and provides only public access.
Benefits: Isolation of change; Easy to use; Improve reusability; security
Principle of encapsulation: Hide content that does not need to be provided externally, hide attributes, and provide public access to them.
This: represents the object, which is a reference to the object that the function belongs to.
What does this stand for? The object that calls this represents the object, and this is a reference to that object.
When do you use this during development?
When defining a function, use this to represent the object that calls the function if the function uses it internally.
This can also be used for calls between constructors.
Call format: this(actual argument);
Member attributes and member methods (generic methods) are called after this object.
This, followed by (), calls the constructor for the corresponding argument in this class.
Note: Calling the constructor with this must be defined on the first line of the constructor. Because the constructor is used for initialization, the initialization action must be performed. Otherwise, the compilation fails.
Static: ★★★ is a modifier used to modify members (member variables and member functions).
Features:
1. To achieve object sharing of common data in an object, you can statically modify the data.
2. Statically decorated members can be called directly by class names. That is, static members have one more way to call. Class name. Static mode.
3. Static is loaded as the class is loaded and takes precedence over the object.
Disadvantages:
1. Some data is object specific and cannot be statically modified. Because then, the specific data becomes the shared data of the object. That’s the wrong way to describe things. Therefore, when defining static, it must be clear whether the data is shared by objects.
2. Static methods can only access static members, not non-static members.
Because static methods exist before objects when they are loaded, there is no way to access members in an object.
3, static methods cannot use this, super keyword.
Because this represents an object, when static, there may be no object, so this cannot be used.
4. The main function is static.
When do you define static members? In other words: when defining a member, should it be statically decorated at all?
There are two types of members:
1. Member variables. (Statically shared data)
Is the data of this member variable the same for all objects:
If so, the variable needs to be statically decorated because it is shared data.
If it is not, then it is said to be specific to the object and stored there.
2. Member functions. (Static when no specific data is called in a method)
What if a member function needs to be statically decorated?
Just refer to whether specific data in the object is accessed within the function:
If there is access specific data, the method cannot be statically decorated.
This method needs to be statically decorated if no specific data has been accessed.
The difference between a member variable and a static variable:
1, member variables belong to objects, so they are also called instance variables.
Static variables are also called class variables because they belong to a class.
2. Member variables exist in heap memory.
Static variables exist in the method area.
3. Member variables exist as the object is created and disappear as the object is reclaimed.
Static variables exist as the class is loaded and disappear as the class disappears.
4. Member variables can only be called by objects.
Static variables can be called by objects as well as by class names.
So, member variables can be called the object’s unique data, and static variables are called the object’s shared data.
Static note: Static has a long lifetime.
Static code block: an area of code block identified by static keywords, defined in a class.
What it does: It initializes a class and executes a block of static code as the class is loaded, but only once (new multiple objects are executed only once). If in the same class as the main function, it takes precedence over the main function.
Public: maximum access permission.
Static: Use the class name instead of the object.
Void: The main function has no return value.
Main: The specific name of the Main function.
(String[] args) : The argument to the main function, which is an array of strings. When the JVM calls the main method, the actual argument passed is new String[0].
The JVM passes an array of strings of length 0 by default, and we can also specify parameters to pass when running this class. You can run the class on the console and add parameters after it. Parameters are separated by Spaces. The JVM automatically stores these string parameters as elements in the ARGS array.
The order of execution when static code block, construction code block, and constructor exist together: static code block – > construction code block – > constructor;
Generate Java help documents: command format: javadoc -d folder name – auther – version *.java
/ * * / / format
* class description
* @ the author the author name
* @ version version number
* /
/ * *
* Method description
*@param Parameter description
*@return Indicates the return value
* /
Design patterns: The most effective ideas for problem solving. Is a set of used, familiar, catalogued code design lessons. Design patterns are used to re-use code, make it easier for others to understand, and ensure code reliability.
There are 23 design patterns in Java:
Singleton design mode: ★★★★★ ★
Problem solved: Ensuring the object uniqueness of a class in memory.
For example, when multiple programs read a configuration file, it is recommended that the configuration file be encapsulated as an object. To facilitate data manipulation and ensure that multiple programs read the same configuration file object, the configuration file object must be unique in memory.
The Runtime() method is designed in a singleton design pattern.
How do you make sure that objects are unique?
Thought:
1. Do not let other programs create this class object.
2. Create a class object in this class.
3. Provide methods for other programs to obtain this object.
Steps:
(1) Since constructor initialization is required to create objects, once the constructor is privatized, no other program can create objects of this class.
Create an object of this class.
3. Define a method that returns the object so that other programs can get the object through the method. (Function: controllable)
Code representation:
1. Privatize constructor.
2. Create private static objects of this class.
3. Define public and static methods that return this object.
Benefits:
1: Improved code reusability.
2: The relationship between classes is generated, providing another premise of characteristic polymorphism.
The origin of the parent class: in fact, it is by a number of classes continue to extract the common content.
Java supports only single inheritance for inheritance. Java does not support multiple inheritance directly, but retains this mechanism for improvement.
Single inheritance: A class can have only one parent.
Multiple inheritance: A class can have more than one parent class.
Why not support multiple inheritance?
Because when a class inherits from two parent classes, both of which parent classes have the same functionality, which one does the subclass object run when it calls that functionality? Because there is a method body in a method in a parent class.
But Java supports multiple inheritance. A inherits B B inherits C C inherits D
With the emergence of multiple inheritance, there is an inheritance system. The top parent class in the system is extracted upwards. It defines the function of the most basic and common content of the system.
Therefore, in order for a system to be used, you can directly refer to the function of the parent class in the system to know the basic usage of the system. So when you want to use a system, you need to create objects. It is recommended to create the subclass object, because the subclass can not only use the functionality in the parent class. You can also use subclass-specific functionality.
To put it simply: For the use of an inheritance system, look up the contents of the top parent class and create the objects of the bottom child class.
After the appearance of the child and parent classes, the members of the class have what characteristics:
1: member variable.
When an object of the subclass type calls the property when the same property occurs in the subclass, the value is the property value of the subclass.
If you want to call an attribute value in a parent class, you need to use a keyword: super
This: represents an object reference of This class type.
Super: refers to the memory space reference in the parent class to which the subclass belongs.
Note: there is no member variable with the same name in the parent class, because once defined in the parent class, the child class does not need to define it, and can inherit it directly.
2: member function.
When an identical method appears in a child class, the subclass object runs the method in the child class. As if the methods in the parent class were overwritten. So in this case, there’s another property of the function: overwrite.
When do you use overrides? When the functional content of a class needs to be changed, it can be done through overwriting.
3: constructor.
When the subclass constructor is found to be running, the superclass constructor is run first. Why is that?
The reason: The first line of all constructors of subclasses actually has an invisible super() statement;
Super (): represents the constructor of the parent class and calls the constructor in the parent class corresponding to the argument. Super (), on the other hand, is the constructor that calls the hollow arguments of the parent class.
Why do subclass objects always need to call functions in their parent class when they are initialized? (Why add super() in the first line of the subclass constructor?)
Because a subclass inherits data from its parent class, it must see how the parent class initializes its own data. So when a subclass initializes an object, it first calls the constructor of the parent class, which is the instantiation process of a subclass.
Note that all constructors in a subclass access the empty argument constructor in the parent class by default, because the first line of each subclass constructor has the default super() statement;
If there is no null argument constructor in the parent class, then the super statement must be used to specify the constructor in the parent class to access.
If a subclass constructor uses this to specify that the subclass’s own constructor is called, then the called constructor will also access the constructor in the parent class.
Question: Can super() and this() occur together in constructors?
Only one of two statements can be defined on the first line, so only one of them can appear.
Super () or this(): Why must it be defined in the first line?
Since both super() and this() call constructors, which are used for initialization, the initialization action is done first.
Details of inheritance:
When to use inheritance?
Inheritance is only possible when there is an ownership relationship between classes. A is a kind of B. A inherits B. The Wolf is a member of the canine family.
In the English book, ownership: “Is a”
Note: Do not inherit just to get existing members from other classes.
Therefore, to judge the ownership relationship, we can simply see that if after inheritance, the functions in the inherited class can be provided by the subclass, then inheritance is valid. If not, it cannot be inherited.
Details of the two:
Two points to note when covering methods:
1: When a subclass overwrites its parent class, it must ensure that the permission of the subclass method is greater than or equal to that of the parent class method to achieve inheritance. Otherwise, the compilation fails.
2: When overwriting, either all static or none static. (Static can only overwrite or be overwritten by static)
One drawback of inheritance is that it breaks encapsulation. Some classes, or their functions, need to be inherited or overwritten.
How do you solve the problem? Introduce a keyword, final: final.
Final features:
1: This keyword is a modifier that modifies classes, methods, and variables.
2: A final class is a final class and cannot be inherited.
3: A final method is a final method and cannot be overridden.
4: A final variable is a constant and can only be assigned once.
In fact, the reason for this is to give some fixed data a more readable name.
Can’t you use it without final? So this value is a variable that can be changed. Add final, the program is more rigorous. When constant names are defined, they have specifications, all letters are capitalized, and if they consist of more than one word, they are joined by an _.
Abstract class: abstract
Abstract: not concrete, not understand. Abstract class representation.
In the process of continuous extraction, the method declaration in the common content is extracted, but the methods are different and there is no extraction. In this case, the extracted method is not specific and needs to be marked by the specified keyword abstract and declared as abstract method.
The class of the abstract method must be marked as abstract, that is, the class must be decorated with the abstract keyword.
Abstract class features:
1: Abstract methods can only be defined in abstract classes. Abstract classes and methods must be modified by the abstract keyword (classes and methods can be described, not variables).
2: Abstract methods define only method declarations, not method implementations.
3: Abstract classes cannot be created objects (instantiated).
4: A subclass can only be instantiated if it inherits an abstract class and overwrites all the abstract methods in the abstract class. Otherwise, the subclass is an abstract class.
Details of the abstract class:
1: Is there a constructor in the abstract class? Yes, used to initialize subclass objects.
2: Can non-abstract methods be defined in abstract classes?
You can. In fact, there is no big difference between abstract class and general class, they are describing things, but when abstract class describes things, some functions are not concrete.
So both abstract classes and general classes, by definition, need to define properties and behaviors.
However, there is one more abstract function than the normal class. And there is one less section for creating objects than normal classes.
3: Abstract keyword and which can not coexist? final , private , static
4: Can abstract classes not define abstract methods? You can. The purpose of the abstract method is simply to prevent the class from creating objects.
Template method design pattern:
Problem solved: While part of the internal functionality is determined, part of the implementation is uncertain. At this point, you can expose the uncertain parts and let subclasses implement them.
This article is reprinted at: Lybyte
Ok, that’s it for today! Hope to give students some help and inspiration, remember to like yo ~