1、Identifiers:标识符
①Names of class,method and variable:用于类名、方法名、变量名
②Begin with character,"_" or "$":标识符不能以数字开头
③Case sensitive:大小写敏感(区分大小写)
④No length limitation:长度无限制
⑤标识符不能是Java关键字,汉字也可以做标识符,但是不建议使用(使用汉字涉及到编码问题,跨平台时回出现问题)。
⑥String是Java的一个类,类名是标识符,所以String可以做标识符。
⑦There is no sizeof operator.Java中没有sizeof运算符,所以sizeof可以作为标识符
⑧关键字、保留字(const、goto、true、false、null)不能用作标识符。
1 public class Identifiers { 2 public static void main(String[] args) { 3 String $abc="abc1"; 4 String _abc="abc2"; 5 //编译错误,标识符不能以数字开头 6 //String 8abc="abc3"; 7 String 中国="China"; 8 String String="wanghao"; 9 int Integer=22; 10 //Java中没有sizeof运算符,所以sizeof可以作为标识符 11 String sizeof="sizeof"; 12 System.out.println(String);//wanghao 13 System.out.println(Integer);//22 14 } 15 }
2、Keywords:关键字
The goto and const keyword are not used in the Java programming.
const 保留关键字(留儿不用)。const是从C++继承而来的,但是Java并不使用const这个关键字,由于JVM是由C++编写的,因此,虽然Java并不使用const这个关键字,但却要保留下来。和const一样goto也是保留关键字!
关键字全部用小写表示(53个)
Strictly speaking,the literals true,false,and null are not keywords but literals;however,the distinction is academic.
true,false,and null是保留字但不是关键字,只是3个值,不能用来做关键字。
3、Primitive data types:基本数据类型
Java强数据类型只能先声明后使用。
boolean Boolean literals indicating true or false
char Stores one 16-bit unicode character
char 同时具备字符和整数的特征。
byte 8-bit integer
short 16-bit integer
int 32-bit integer
long 64-bit integer
Integer Data Types-byte,short,int and long
float 32-bit floating-point number
double 64-bit floating-point number
在范围内的常数可以自动转换类型,但是变量就不一定了。
小————》大 自动转换
大————》小 强制类型转化
Byte相当于截取了最后8位,前面的全部舍去
1 public class Demo { 2 public static void main(String[] args) { 3 int i1 = 5; //语句1 Right 4 5 byte b1=5; //语句2 Right 6 byte b2=3; //语句3 Right 7 //byte b3=128; //语句4 Wrong 128超出了byte的范围 8 byte b3=(byte)128; //语句5 Right 9 //byte的取值范围:(-128,127) 10 byte b4=127; 11 byte b5=-128; 12 //byte b6=-129; //-129超出了byte的范围 13 14 long l1=5; //语句6 Right 15 long l2=5L; //语句7 Right 16 //b1=i1; //语句8 Wrong i1不能转换成b1 17 b1=(byte)i1; //语句9 18 l1=i1; //语句10 Right 19 //i1=123L; //语句11 Wrong 20 i1=(int)123L; //语句12 21 22 //b1=b1+b2; //语句13 Wrong b1+b2变成int 23 b1=(byte)(b1+b2); //语句14 24 } 25 }