1.什么是标识符
- Java所有的组成部分都需要名字。
- 类名,方法名,变量名 都是标识符。
2.关键字 - 特殊的标识符
- java中的关键字是有特殊含义的标识符,是有特定功能的,是被保留使用的。
- 不可以用关键字来作为类名,方法名或变量名。
/**
* 下面是常见的 关键字
*/
访问修饰符: public private protected
类与接口相关 : class interface abstract extends implements new super this instanceof
数据类型相关 :void byte short int long float double char boolean enum null true false
多线程相关 : synchronized volatile 【难点】
异常相关 :throw throws try catch finally
返回值 : return
条件语句相关 :if-else switch-case break continue default while do for
包相关 : package import
断言 : assert(不常用)
调用底层代码 : native(不常用)
不可变的,静态的 : final static
3.标识符的书写规范
- 大小写敏感!
- 以
字母(a-z A-Z)
、美元符号 $
、下划线 _
开头!- 首字母之后 可以是
字母(a-z A-Z)
、美元符号 $
、下划线 _
、数字
的任意组合!- 不可以使用关键字作为标识符!
- 可以使用中文,但不推荐这样做。
/**
* 1. 下面展示正确的标识符命名方式
*/
String myname = "northcastle"; // 小写字母开头
String Myname = "northcastle"; // 大写字母开头
String $myname = "northcastle"; // $ 符号开头
String _myname = "northcastle"; // _ 开头
String 我的名字 = "northcastle"; // 中文字符作为标识符
// 首字符后可以放 字母、数字、下划线、$的任意组合
String m_y$n2ame$_1 = "northcastle";
String M_y$n2ame$_1 = "northcastle";
String $_y$n2ame$_1 = "northcastle";
String __y$n2ame$_1 = "northcastle";
String _我的名字 = "northcastle"; // 中文字符作为标识符
/**
* 2.下面展示不正确的标识符命名方式
*/
String 3myname = "northcastle"; // 数字开头不可以
String *myname = "northcastle"; // 非_、$ 的特殊字符开头不可以
String static = "northcastle"; // 使用关键字不可以
4.完成
Congratulations!
You are one step closer to success!