Java re-learning important knowledge points summary
There should be a table of contents in the upper right corner of the page, I do n’t know if you can see it
Numbers can be separated by underscores so that we can count the digits
static modifier
final modifier constant
- final modified class cannot be inherited
Bit operation
String position causes different output
Wildcard
* 为通配符
hasNext ()… determine related applications of input content
Switch to pay attention to case penetration and write to break;
The difference between while and do while
do while executes first, then judges, so execute at least once
for loop and enhanced for loop
It is recommended to use a for loop for arrays. Subscripts are faster and
related summary. There should be a table of contents in the upper right corner to find them faster
Xunhua label
Value passing and reference passing
- By value transfer, a copy of the value of the original variable a is assigned to the new variable b. Modify the value of the new variable b without changing the value of the original variable a.
- Pass by reference, you can experience when using the method to change the property value of the object, as shown below
Method overloading
Passing parameters on the command line
Command line execution can not find or can not load the main class, roll back the directory to the upper level of the package, and then execute with the full package name
Variable parameters of the method
Memory analysis
Array initialization method
toString method output
Common methods of Arrays class
Sparse array (the principle of compression)
OOP object-oriented programming
OO means object-oriented
Constructor also construct method
Package
High cohesion, low coupling
inherit
- final modified class cannot be inherited
- All classes inherit the Object class by default
- Private cannot be inherited
- Subclasses use the superclass constructor by default, and super () defaults to the first line in the subclass constructor
super、this
Method rewriting
-
StaticMethods andNon-staticThe method is different
-
Static methods, method calls are only related to the left
-
Non-static method, rewritten. The subclass overrides the parent class method and executes the subclass method
Relationship between polymorphic methods
- When the parent class reference points to a child class object, both the child class and the parent class have methods (the child class overrides the parent class's method).
instanceof
- Only the inheritance relationship can be compared, otherwise directly report an error
Code block
- An anonymous code block, before the constructor (method)
- Static code block, executed directly when the class is loaded, and only once
- Static code block is executed only once
Static import package
-
Before the package is imported, the class name is called by the method name
-
After guiding the package, use it directly
Abstract class, abstract method
- In the abstract classAbstract methodYou only need to write the method name, not the method body (because the abstract method must be rewritten by the subclass)
- You can define common methods in an abstract class. You must have a method body to define common methods, and you can override or not override them in subclasses. If not overridden, the method of the parent class is called.
- Abstract classes cannot be new objects, they can only be inherited
- AbstractAbstract methodMust be overridden by subclasses
- If there is an abstract method in a class, then this class must be modified by abstract (that is, this class must be an abstract class)
interface
- Only methods can be declared in the interface
- The class that implements the interface must override the method in the interface
- Interface can inherit more
- The attribute value defined in the interface is added by default
public static final
, which is a constant (generally not so) - The methods defined in the interface are added by default
public abstract
- Interface cannot be instantiated, is not a class, and has no constructor
Inner class
- Non-static inner classes can obtain private properties of outer classes and use methods of outer classes
- Static inner class, unable to access non-static attributes of outer class
- Classes written in methods are local inner classes
- Anonymous inner class
- The new interface, followed by rewriting the method, returns the class that implements the interface
abnormal
- Five keywords
- try
- catch
- finally will be executed
- throw
- throws
Try to catch finally
- Once the statement in try is abnormal, jump to the catch statement immediately.
- Type the type of exception to be caught in the brackets
- The highest type of exception is Throwable, including Error and Exception
- Finally, it will be executed regardless of whether there is a try or an exception. It is usually used to handle the aftermath work, such as closing the stream, etc.
- finally can not write
- Use multiple catch statements when catching multiple exceptions, but the range of exceptions in parentheses should be from small to uppercase. (Only one catch statement will be executed. Once the exception in parentheses is satisfied, other catch statements will not be executed)
Exception printing in catch
e.printStackTrace()
Print out what exceptions you caught (almost the same as when you don't try the system to automatically pop up), usually add a line of your own output information in front.
if (b==0){ //主动抛出异常
throw new ArithmeticException();
}
[Reference]
B Station God