3. JAVA method local variables member variables

1. Variables

1.1 Concept
A number that can be changed is called a variable. In the Java language, all variables must be declared before use.
Generally, a variable is described by the three parts of "variable type variable name = variable value;". Such as: int a = 3;
the principle of variable use: the principle of proximity, that is, try to control the scope of use of variables to the minimum

1.2 Local variables

Location: Defined in the method or on the declaration of the method Note
: Must be manually initialized to allocate memory. For example: int i = 5; or int i; i = 5;
released

1.3 Member variables

Position: Defined outside the method in the class
Note: Without initialization, it will be automatically initialized to the default value
Life cycle: In the entire class, the member variable will only be released when the class disappears

insert image description here

1.4 Exercise: Testing the Default Value of a Variable

Create package: cn.tedu.basic
Create class: TestVariable1.java

package cn.tedu.design;
/*本类用于测试各种类型变量的默认值*/
public class TestVariable1 {
    
    
        static String name;
        static byte b;//整数类型默认值都是0
        static short c;
        static int d;
        static long e;
        static float f;//小数类型的默认值是0.0
        static double g;
        static char j;//char类型的默认值是\u0000
        static boolean h;//boolean类型的默认值是false

        public static void main(String[] args) {
    
    
            System.out.println(name);//null,引用类型的默认值
            System.out.println(b);
            System.out.println(c);
            System.out.println(d);
            System.out.println(e);
            System.out.println(f);
            System.out.println(g);
            System.out.println(h);
            System.out.println(j);
            System.out.println(h);
        }
    }

1.5 Exercise: Local and Member Variable Testing

Create package: cn.tedu.basic
Create class: TestVariable2.java

package cn.tedu.oop;
/**本类用于测试变量的使用*/
public class TestVariable2 {
    
    
    //2.定义成员变量:
    //1)位置:类里方法外
    //2)无需手动初始化,会自动赋予对应类型的默认值
    //3)作用域:在整个类中生效,类消失,变量才会消失
    static int count;
   
    //3.变量有一个使用的原则:就近原则
    static int sum = 200;
    public static void main(String[] args) {
    
    
        //1.定义局部变量:
        //1)位置:在方法里/局部代码块里
        //2)必须手动初始化
        //3)作用域:在方法/局部代码块中,对应的代码执行完局部变量就被释放
        int sum = 100;//定义在方法中的局部变量sum
        System.out.println(sum);//变量的就近原则:使用的都是自己附近的变量,100
        System.out.println(count);
       
        for (int i = 0; i < 10; i++) {
    
    //局部变量i只能在循环中使用
            System.out.println(i);
        }
        //System.out.println(i);//报错:无法引用变量i:i cannot be resolved to a variable
    }
}

2 methods

2.1 Overview
Named code blocks and methods can have parameters or not, which can improve code reusability.

2.2 Format of method definition

insert image description here

2.3 Method call sequence diagram

Execute the code sequentially, call the specified method, and return to the calling location after execution is complete

insert image description here

2.4 Exercise: Test method call sequence/parameters/return value

Create package: cn.tedu.method
Create class: TestMethod .java

package cn.tedu.method;
/**本类用于测试方法*/
public class TestMethod {
    
    
	//1.创建程序的入口函数main()
	public static void main(String[] args) {
    
    
		System.out.println(1);
		/**2.我们通过方法名+参数列表的方式来调用方法的功能*/
		method1();//调用method1()
		System.out.println(2);
		method2(3);//调用method2()
		int result = method3(1,2);//调用method3()
		System.out.println(result);
	}

	/**3.如果方法想要返回值,必须修改返回值类型
	 * 并且return对应类型的结果
	 * 如果方法的返回值类型是void,不允许有返回值
	 * */
	/*本方法用来测试方法的返回值类型*/
	public static int method3(int i, int j) {
    
    
		/**4.通过return关键字将方法结果返回到调用位置*/
		return i+j;
	}

	/**1.方法的修饰符 方法的返回值类型 方法名(方法参数){方法体}*/
	/*method1()想测试方法的调用顺序*/
	public static void method1() {
    
    
		System.out.println(5);
		System.out.println(6);
		System.out.println(7);
	}
	
	/*本方法用来测试方法的参数,参数的位置在小括号里*/
	public static void method2(int a) {
    
    
		System.out.println("海绵宝宝今年:"+ a +"岁啦~");
	}	
}

2.5 Method Overloading

Method overloading refers to defining multiple methods with the same name in a class, but the parameter list of each method is different (that is, the number and type of parameters are different). When the program calls the method, it can pass to them. Different numbers and types of parameters to determine which method to call.

2.6 Exercise: Test Method Overloading

Create package: cn.tedu.method
Create class: TestMethodOverload.java

package cn.tedu.method;
/**本类用于测试方法的重载*/
public class TestMethodOverload {
    
    
	public static void main(String[] args) {
    
    
		/**1.我们根据方法名+参数列表确定具体调用哪个方法*/
		/**2.方法的重载:
		 * 在同一个类中,存在方法名相同,但参数列表不同的方法
		 * 如果在同类中,同名方法的参数个数不同,一定构成重载
		 * 如果在同类中,同名方法的参数个数相同,
		 * 需要查看对应位置上参数的类型,而不是参数名,与参数名无关
		 * (int a,String b)与(int b,String a)--不构成重载
		 * (int a,String b)与(String a,int b)--构成重载
		 * */
		//2.调用method()
		method();
		//4.调用method(int)
		method(666);
		//6.调用method(int,String)
		method(888,"泡泡");
	}

	//1.创建一个无参的method()
	public static void method() {
    
    
		System.out.println("哈哈哈哈我没参数");
	}
	//3.创建一个method(int n)
	public static void method(int n) {
    
    
		System.out.println("哈哈哈哈我的参数是:"+n);
	}
	//5.创建一个method(int n,String s)
	public static void method(int a,String b) {
    
    
		System.out.println(b+"今晚要干他"+a+"碗大米饭");
	}
	public static void method(String a,int b) {
    
    
		System.out.println(b+"今晚要干他"+a+"碗大米饭");
	}
}

3 The difference between extended member variables and local variables

insert image description here

Guess you like

Origin blog.csdn.net/weixin_58276266/article/details/131460729