1. 程式人生 > 其它 >Java變數複習(2)

Java變數複習(2)

Java變數複習(2)

一、命名規則

/**
 * FileName:      NamingNotations.java
 * @Description: TODO(用一句話描述該檔案做什麼)
 * All rights Reserved, Designed By Gujiakai
 * Copyright:    Copyright(C) 2021-2022
 * Company       Nanjing Xiaozhuang University
 * @author:      Gu Jiakai
 * @version      V1.0 
 * Createdate:   2021年6月27日 下午2:28:32 
 *
 * Modification  History
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2021年6月27日      Jaya          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package variable;

/**
 * @ClassName:      NamingNotations.java
 * @Description:    
 * 1. int a_;
 * 2. int a@;
 * 3. int a3;
 * 4. int 8@;
 * 5. int 9_;
 * 6. int X$_;
 * 7. int y;
 * 8. int _$_;
 * 9. int $_$;
 * 10. int $*$;
 * 11. int $1$;
 * 12. int _1_;
 * 13. int _@_;
 * 14. int a#;
 * 15. int a";
 * 16. int 123a";
 * 17. int 123a_;
 * 18. int $123b_;
 * @author          Gu jiakai
 * @version         V1.0  
 * @Date            2021年6月27日 下午2:28:32  
 */
public class NamingNotations {
public static void main(String[] args) {
	/*
	 * 變數命名只能使用字母、數字、$、_
	 * 變數的第一個字元,只能使用字母、$、_,不能使用數字。
	 */
	int a_=1;//合法。
//	int a@=1;不合法,變數名裡面含有@。
	int a3=1;//合法。
//	int 8@=1;不合法,變數名第一個不能是數字,變數名裡面含有@。
//	int 9_=1;不合法,變數名不能以數字開頭。
	int X$_=1;//合法。
	int y=1;//合法。
	int _$_=1;//合法。
	int $_$=1;//合法。
//	int $*$=1;不合法,變數名裡面含有*。
	int $1$=1;//合法。
	int _1_=1;//合法。
//	int _@_=1;不合法,變數名含有@。
//	int a#=1;不合法,變數名含有#。
//	int a"=1;不合法,變數名含有"。
//	int 123a"=1;不合法,變數名不能以數字開頭,變數名裡面含有"。
//	int 123a_=1;不合法,變數名不能以數字開頭。
	int $$123b_=1;//合法。	
}
}

二、作用域

/**
 * FileName:      ActionScope.java
 * @Description: TODO(用一句話描述該檔案做什麼)
 * All rights Reserved, Designed By Gujiakai
 * Copyright:    Copyright(C) 2021-2022
 * Company       Nanjing Xiaozhuang University
 * @author:      Gu Jiakai
 * @version      V1.0 
 * Createdate:   2021年6月27日 下午4:09:58 
 *
 * Modification  History
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2021年6月27日      Jaya          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package variable;

/**
 * @ClassName:      ActionScope.java
 * @Description:    
 * 屬性的作用域在方法中,引數的作用域也在方法中,
 * 如果屬性和引數命名相同了的話? 那麼到底取哪個值?
 * @author          Gu jiakai
 * @version         V1.0  
 * @Date            2021年6月27日 下午4:09:58  
 */
public class ActionScope {
	int i=1;
	
	public void method1(int i)
	{
		System.out.println(i);
	}
	public static void main(String[] args){
	new ActionScope().method1(5);//輸出是5,當訪問的變數被多個作用域影響的時候,按照就近原則取。
	}
}

//result:
//5

三、變數final

/**
 * FileName:      $finalModifyParameter.java
 * @Description: TODO(用一句話描述該檔案做什麼)
 * All rights Reserved, Designed By Gujiakai
 * Copyright:    Copyright(C) 2021-2022
 * Company       Nanjing Xiaozhuang University
 * @author:      Gu Jiakai
 * @version      V1.0 
 * Createdate:   2021年6月27日 下午4:27:21 
 *
 * Modification  History
 * Date         Author        Version        Discription
 * -----------------------------------------------------------------------------------
 * 2021年6月27日      Jaya          1.0             1.0
 * Why & What is modified: <修改原因描述>
 */
package variable;

/**
 * @ClassName:      $finalModifyParameter.java
 * @Description:    
 * 如果final修飾的是引數,能否在方法裡給這個引數賦值?
 * @author          Gu jiakai
 * @version         V1.0  
 * @Date            2021年6月27日 下午4:27:21  
 */
public class $finalModifyParameter {
	public void method1(final int j)
	{
//		j=5;
//		不可以,因為在呼叫方法的時候,就一定會第一次賦值了,後面不能再進行多次賦值。
	}
}

//result:
//The final local variable j cannot be assigned. 
//It must be blank and not using a compound assignment

四、表示式

五、塊