[java]--Literal quantity and variable

Posted by punk3d on Thu, 03 Feb 2022 20:01:17 +0100

1. Literal quantity: Literal quantity is data/value. Programming is in line with life. There are many numbers in life, such as your weight is 70 kg, your height is 172 cm, the price of watermelon is 3 yuan/kg, etc. These are literal quantity (data/value). A programming language must be able to represent data before it can process data, so Java programs express data by literal quantity.

  1. Software processes data
  2. Each data has a data type, and different data types occupy different memory
  3. Literal data will be stored in memory for a temporary space, but at this time the literal memory cannot be reused. If reuse is required, variables are needed.
  4. data type
    1. Integer Type
    2. Floating Point
    3. Character type (text, single character)
    4. Boolean (true and false): true, false
    5. String (text, multiple characters): String

2. Variables: Variables are literal quantities that define a block of memory for later better reuse

  1. Three elements of a variable
    1. Literal quantity (value)
    2. data type
    3. Variable Name
  2. Use of variables
    1. Declare variables: Syntax format:'Data type variable name;'
    2. Assigning variables: Syntax format: "variable name = value;"
    3. Combination
      1. Data type variable name = value; PS: Declarations and assignments can be done on the same line
      2. Data type variable name 1 = value 1, variable name 2 = value 2, variable name 3 = value 3; PS:Java allows multiple variables of the same type to be declared at once
    4. Matters needing attention
      1. Declaration before assignment
      2. =as assignment operator, right priority is high, execute first, assign execution result to left variable
      3. You cannot declare two variables with the same name in the same brace
        1. In the same brace, it means that in the same field of memory, there are two variables named age on the same area, which is not allowed because the program is running. The java virtual machine doesn't know which variable to visit either (for example, you have two friends whose names are Zhang San, and when the three of you are present at the same time, you call Zhang San, but they don't know who you are!). Therefore, variable names cannot be renamed in the same domain, but remember that variables can be reassigned.
      4. Different domains can have variables with the same name
  3. Categorization of variables: Variables are categorized according to where they are declared
    1. Local variables: variables declared in the body of the method
      1. PS: The local variable is only valid in the body of the method and the memory of the variable is released when the body finishes executing
    2. Member variables: variables declared within a class
      1. Modify with static keyword: static member variable (referred to as static variable)
      2. No static keyword: is an instance member variable (for short, an instance variable)
  4. Scope of variables: Valid range of variables
    1. Don't recognize braces
      1. public class VarTesto7{
        	 public static void main(String[] args){
        		int k=100;
        	 }
        	 public static void m(){
        		System.out.println(k);
        	 }
        }
        The above code defines a variable k in the main method, which is a local variable and is only valid in the main method, so the local variable k cannot be accessed in the m method
      2. public class VarTesto7{
        	 static int k=100;
        	 public static void main(String[] args){
        		System.out.println("main k="+k);
        	 }
        	 public static void m(){
        		System.out.println(k);
        	 }
        }

        The k-code in the above code is defined in the class body, so both the main method and the m method can access the K variable. But at this time, only K in main method is output, not K in M method. This is because m method is not executed, program execution is performed from main method as entry, m method is not called manually in main method, so m method will not execute.

    2. Nearest principle: automatically accesses the data closest to it
      1. public class VarTest07{
        	static int k= 100;
        	public static void main(String[] args){
        		int k=300;
        		System.out.println("main k="+k);
        	}
        	public static void m(){
        		System.out.println(k);
        	}
        }

        The above code defines two K variables with the same name, because one is in the class body and the other is in the method body, so there is no error. The result of running at this time is "main k=300" instead of 100, which indicates that Java programs follow the "proximity principle". That is, if the above program accesses the variable K in the main method, it accesses the local variable K instead of the static variable K.

      2. public class VarTest07{
        	static int k= 100;
        	public static void main(String[] args){
        		int k=300;
        		System.out.println("main k="+k);
        		m();
        	}
        	public static void m(){
        		System.out.println(k);
        	}
        }

        This code main method calls the m method and you can see that the static variable k is accessed in the m method because the output is 100, not 300.

3. Chapter exercises: Describe the student's information through variables, including number, name, gender, height. The school number is integer, the name is string, the gender is character type, and the height is floating point type. There are two copies of the specific student data, the first student information is: school number 110, name Zhang San, male sex, height 1.85 meters. The second student information is: school number 120, name Li Si, gender female, height 1.65 meters. Requires that the student's information be eventually output to the console. The output is as follows:

School number: 110
 Name: Zhang San
 Gender: Male
 Height: 1.85
-------------------------------
School number: 120
 Name: Li Si
 Gender: Women
 Height: 1.65

Your own answer:

public class VarTest07{
	public static void main(String[] args){
		int studynumber;
		double high;
		String name;
		char sex;
		studynumber=110;
		high=1.85;
		name="Zhang San";
		sex='male';
		System.out.println("School Number:"+studynumber);
		System.out.println("Full name:"+name);
		System.out.println("Gender:"+sex);
		System.out.println("Height:"+high);
		System.out.println("-------------------------------");
		studynumber=120;
		high=1.65;
		name="Li Si";
		sex='female';
		System.out.println("School Number:"+studynumber);
		System.out.println("Full name:"+name);
		System.out.println("Gender:"+sex);
		System.out.println("Height:"+high);
	}
}

Topics: Java