Cha_ 02 Java Language Base

Cha_ 02 Basic Java Language (1)

This is the video I watched and learned from Station B, [Java Zero Foundation Tutorial Video (for Java 0 Foundation, Introduction to Java)]( Java Zero Foundation Tutorial Video (for Java 0 Foundation, Introduction to Java)_ Bell-Bell-Bell_ bilibili Notes from P23 - P31. The knowledge points and codes were not extracted exactly according to the contents of the video, and some modifications were made. Just remember, it's mainly convenient for you to review later. Fundamentals or more important Ha~

Identifiers

About identifiers in the Java language

1. What is an identifier?

( Programmers can customize named words as identifiers.
( Identifiers can identify: class name, method name, variable name, interface name, constant name...

2. Naming rules for identifiers? (Naming rules are the language of programming languages, do not conform to rules, compiler will error)

  • A valid identifier can only be numbered (09), letters (AZ, a~z), underscores () It is composed of a dollar sign ($) and cannot contain other special symbols (spaces are also a symbol).
  • Cannot start with a number
  • Strict case sensitivity
  • Keyword (public, main, etc.) cannot be an identifier
  • Theoretically no length limit

3. Naming specifications for identifiers? (Naming conventions are not part of the Java language syntax, do not follow the conventions, and the compiler will not error)

  • Better to see the name when naming

    public class UserService{
    	public void login(String name,String password){
    		
    	}
    }
    
  • Follow the hump nomenclature
    For example: OuterClass, outerObject, StaticNestedClass, nestedObject, etc.

  • Class name, interface name: first letter uppercase, followed by each word first letter uppercase;

  • Variable name, method name: the first letter is lowercase, followed by each word in uppercase;

  • Constant Name: All capitals, with underscores () between words if they are composed of more than one word Separate.

    //IdentifierTest01 is a custom class name that can be modified
    public class IdentifierTest01 
    {
    	//mian is the entry to the program and cannot be modified. args is the variable name
    	public static void main(String[] args){
    		
    	}
    }
    

2. Keyword

The keywords in the Java language include

1. Data type

​ byte char short int float long double boolean class interface enum

2. Constant Value

​ true false null

3. Process Control

​ if else do while for switch case default break continue return try catch finally assert

4. Modifiers

​ public protected private final void static abstract strictfp transient sychronized volatile native

5. Action

​ package import throw throws extends implements this super instanceof new

6. Reserved Words

( Goto const (meaningless in Java, meaningful in assembly)

All keywords in Java are lowercase

3. Literal Value

( Literal value is the literal data value. It is called literal value in Java and constant in C.

  • Literal values (const) such as: 10, 3.14,'const','a', true;

  • Literal values are data;

  • Literal values, identifiers, and keywords are part of the Java source program.

  • Data is categorized in the real world, so there are different types of data in the computer world, such as:

    • Integer literal values: 10, -10;
    • Floating-point literal value: 3.14;
    • Boolean literal values: true, false;
    • String literal value:'const','literal value';
    • Character literal values:'c','value';
  • Be careful:

    • The string is enclosed in double quotation marks with a half angle, such as "This is a string."

    • Characters are enclosed in single quotation marks with a half angle and can only be single characters, such as:'This is a char.'.

      public class ConstTest01{
      	public static void main(String[] args){
      		
      		//String literal values should be enclosed in half-angle double quotation marks ("")
      		System.out.println("This is a string.");
      		
      		//The literal value of a character type should be enclosed in half-angle single quotation marks ('') and can only be one character. Will the subcompiler fail?
      		System.out.println('c');
      		
      		//Integer, floating point, and Boolean literal values can be written directly
      		System.out.println(111);
      		System.out.println(3.14);
      		System.out.println(true);
      	}
      }
      

IV. Variables

( Data takes up a certain amount of memory in your computer. When you output one data at a time, a temporary memory space is automatically generated to store the temporary data. In order to continuously access this block of memory space, we need to declare a variable to create a fixed memory space.

1. The concept of variables?

  • Variables are essentially a block of memory space, including data types, names, and literal values (data);
  • Variables are the most basic unit of memory for storing data.
  • The specific data stored in the variable should be the same as the data type of the variable. If not, the compilation will fail.

2. The role of data types?

  • Different data types have different data types, and computers allocate different sizes of memory space to store data based on different data types.

3. Declarations and assignments of variables

(1) Syntax format for declaring/defining variables

( Data type variable name;
( Data type variable name 1, variable name 2, variable name 3;

( For example: String name,;, int age,height

( Variable names should conform to identifier naming rules

(2) Grammatical format of variable assignment

( Variable name = literal value;

( For example: age=21;, name="LHH";

( Requirements: Literal values must have the same data type as variables.
( Where = is an operator called an assignment operator. The assignment operator first runs the expression to the left of the equal sign, then assigns the result of the calculation to the variable to the left.

(3) Declarations and assignments can be made simultaneously

( For example: String name = "LHH"; int age = 21.

(4) Variable values can be converted
  int i=10;
  System.out.println(i);//10

  int i=100;
  System.out.println(i);//100

  int i++;
  System.out.println(i);//101

4. Access to variables

(1) How variables are accessed
  • get the specific data stored in the variable, such as System.out.println(i))
  • Modify the specific data (set) stored in the variable, such as i++;.
(2) Variables in Java must be declared and assigned before they can be accessed

( When a variable is declared, the program does not open up a memory space for it, so variable i is not initialized and cannot be accessed directly.

(3) In a Java program, code is executed strictly in top-down order within a method

( If executed directly

  public class VarTest01{
     public static void main(String[] args){
            
      //Declare an int-type variable, i
      int i;
            
      //Output i directly, compilation will error because variable i is not initialized
      //System.out.println(i);
            
      //Assign a value to variable i, I completes its initialization here, and opens up memory space
      i=10;
      System.out.println(i); //10
            
      //The value of a variable can change
      i++;
      System.out.println(11);//10
            
      //Multiple variables can be declared at the same time
      int height,weight,age=21;
            
      //Compilation error because variable height was not initialized
      //System.out.println(height);
            
      //Compilation error because variable height was not initialized
      //System.out.println(weight);
            
      System.out.println(age);//21
    }
  }

5. Scope of variables

Scope concept
  • The scope of a variable is used to describe the valid range of a variable in a program.

  • Variables are accessible only within their scope.

  • Variable names cannot be renamed in the same scope. Duplicate names in different scopes

    public class VarTest02{
        //A class (static) variable age of type int declared is 21
        // *Static-modified variables become static or class variables, which can be accessed in a class
        static int age=21;
        
    	public static void main(String[] args){
    		//The variable name is scoped within the main method body and can only be accessed in main()
            String name="LHH";
            System.out.println(name);//LHH
            
            name="FYY";
            System.out.println(name);//FYY
            
            //Variable names cannot be renamed repeatedly within the same scope
            //System.out.println(name);
            
            //In the for loop, the scope of the variable
            for(int i=0;i<10;i++){
                //In this for loop, the scope of variable i is from the beginning to the end of the for loop
            }
            //Cannot access variable i in for loop
            //System.out.println(i);
            
            int j;//j is scoped within the main() method body
            for(j=0;j<10;j++){
                
            }
            System.out.println(j);//Output 10
            
    	}
        
        public static void doSomething(){
            
            //Compilation error, unable to access name in main()
            //System.out.println(name);
            
            System.out.println(age);
        }
    }
    

6. Classification of variables

Depending on where the variable is declared
  • local variable
    Variables declared in the body of a method are called local variables.

  • Member variables
    Variables declared outside a method (within a class body) are called member variables.

    public class VarTest03{
        
        //Member variable, scoped only in the VarTest03 class body
        Stirng name = "LHH";
        
        //Main method, entry to program
        public static void main(String[] args){
            
            //Local variable, scoped only in main() method body
            Stirng birthday = "09.17";
            
            //Value of variable birthday in output main method
            System.out.println(birthday);//09.17
        }
        
        //Member variable, scoped only in the VarTest03 class body
        String birthday="August 20";
        
        //You cannot write Java statements directly in a class body other than declaring variables
        //System.out.println(birthday);
        
        public static void doSomething(){
            
            //Local variable, scoped only in the body of the doSomething() method
            String birthday="9 17 January";
            
            //Output doSomething() method body total birthday value
            System.out.println(birthday);//September 17
            
        }
        
    }
    

Keywords: Java

Added by MrCool on Wed, 19 Jan 2022 16:32:12 +0200