JAVA learning lesson 9 (keyword 1): java learning keywords
Keywords:
This keyword
This uses 1:
Directly explain in code:
Class Man {private int age; private String name; Man () {name = null; age = 0;} Man (String n) // print "BLF, 0 ", but changing n to name will print null, 0 {name = n;} // changing to name will increase readability, but when the parameter name is added to the stack, the stack has a local variable name, so the name assignment only assigns itself to itself. Like int x = 5; x = x;/* Man (String name) {this. name = name; // The role of this keyword is to say that the name of this object will be heap into the memory and will not conflict} */Man (int) {age = a;} Man (int a, String n) {age = a; name = n;} void run () {System. out. println (name + "," + age); // this is omitted here. name + "," + this. age} public class Main {public static void main (String [] args) {Man jo = new Man ("BLF"); jo. run ();}}
Therefore, when the names of local variables and member variables are duplicated, you can use the keyword "this" to distinguish them. this represents the current object (this is the reference of the object to which the function belongs ):
This indicates the jo object to which the (constructor) Man belongs. Which object calls the function of this, then this indicates the object.
PS: only the object can call the constructor
Therefore, regular and highly informative code
class Man{private int age;private String name;Man(){name= null;age = 0;}Man(String name){this.name = name; }Man(int age){ this.age = age; }Man(int age,String name){this.age = age;this.name = name;}void run(){System.out.println(this.name+","+this.age);}}public class Main {public static void main (String[] args) {Man jo = new Man("BLF");jo.run();}}
Explain this with a memory distribution chart
This usage 2:
This can call other constructors in the constructor.
Error Demo:
Class Man {private int age; private String name; Man () {name = null; age = 0;} Man (String name) {this. name = name;} Man (int age) {this. age = age;} Man (int age, String name) {// it has been written above. Can it be called? The answer is no this. man (name); // this indicates the object. To use an object, you must initialize it first, but this. man (age); // It is used before initialization, so it is incorrect} void run () {System. out. println (this. name + "," + this. age) ;}} public class Main {public static void main (String [] args) {Man jo = new Man (12, "BLF "); // here, two parameters are set to jo. run ();}}
Modify
Class Man {private int age; private String name; Man () {name = null; age = 0;} Man (String name) {this. name = name;} Man (int age) {this. age = age;} Man (int age, String name) {this (name); // call the preceding constructor. Note: The first line must be placed, house this. age = age; the following is an error. For the reason, see this. age = age;} void run () {System. out. println (this. name + "," + this. age) ;}} public class Main {public static void main (String [] args) {Man jo = new Man (12, "BLF"); jo. run ();}}
Note: this rule: this can only be defined in the first line of the constructor, because initialization must be performed first.
The following code:
Man (int age, String name) {this. name = name; // initialize BLFthis. age = age; // initialize 12 this ("gggg"); // The error code returned when BLF is changed to gggg}
PS: this () is not recommended.
This application
Import java. security. cert. trustAnchor; class Man {private int age; private String name; Man () {name = null; age = 0;} Man (String name) {this. name = name;} Man (int age) {this. age = age;} Man (int age, String name) {this (name); // call the preceding constructor. Note: see this. age = age;} public void run () {System. out. println (this. name + "," + this. age);}/* determine whether two people are of the same age */public boolean judge (Man g) {return this. age = g. age; // here this represents jo2} public class Main {public static void main (String [] args) {Man jo = new Man (12, "BLF "); man jo2 = new Man (12, "BLF2"); boolean flag = jo2.judge (jo); if (flag = true) System. out. println ("YES"); else System. out. println ("NO ");}}
Java keywords
Strictly speaking, true, false, and null are not keywords (keyword), but reserved words (reserved). They are actually constants (literal ).
Errors may occur.
Reference: download.oracle.com/...s.html
Java keyword Question 1
Break
A Java keyword used to change the execution process of a program. The execution starts immediately from the next sentence of the current statement. If there is a tag next to it, it will be executed from where the tag corresponds.
Case
Java keywords are used to define a set of Branch selection. If a value is the same as the value given in the switch, it is executed from this branch.
Catch
A keyword of Java, used to declare a block that runs when a runtime error or non-runtime exception occurs in the try statement block.
Char
A keyword of the Java language used to define a character type.
Continue
A Java keyword used to interrupt the current loop process and re-execute the code from the end of the current loop. If there is a tag next to it, it is executed from where the tag corresponds.
Do
A Java keyword is used to declare a loop. The ending condition of this loop can be set through the while keyword.
Else
A keyword in the Java language. if the condition of the if statement is not met, the statement is executed.
For
A Java keyword used to declare a loop. Programmers can specify the statements to be cyclic, introduce conditions, and initialize variables.
If
A keyword of Java programming language used to generate a condition test. if the condition is true, the statement under if is executed.
Instanceof
The Java (TM) Language keyword of a Two-operand is used to test whether the runtime type of the first parameter is compatible with the second parameter.
Return
A keyword of the Java language used to end the execution of a method. It can be followed by a value required in the method declaration.
While
A keyword of the Java language used to define a repeating statement. The exit condition of the loop is part of the while statement.
About break and continue.
The continue statement is related to the break statement, but is rarely used. The continue statement is used to start the next loop of the for, while, Or do-while statement. In the while and do-while statements, the execution of the continue statement means that the test part is executed immediately. In the for loop statement, the execution of the continue statement means that the control is passed to the incremental part.
Java has no keyword default. If the class is set to the default type before definition, it indicates that the access to the class is within the package!
I hope my answer will be helpful to you!