①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)不能用作标识符。

《Java基础知识总结》系列是对自己学习Java历程中知识的一个总结,也是为自己找工作前知识的回顾,为找工作奠定基础。 

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 }

 :关键字

  The goto and const keyword are not used in the Java programming.

保留关键字(留儿不用)。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:基本数据类型

强数据类型只能先声明后使用。

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

  在范围内的常数可以自动转换类型,但是变量就不一定了。

  小————》大  自动转换

  大————》小  强制类型转化

相当于截取了最后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 }

 程序分析:

  ① 整形数据默(byte,short,int and long)认类型是int型,因此语句1正确

如:语句6是由int---->long(自动转换)。

11是由long---->int(不能进行转换,提示“可能损失精度”的错误),必须通过强制类型转换,改正后为语句12。

③ 语句2和语句3是由int---->byte(自动转换),这又是怎么回事呢?

15和20都在byte范围内但是15*20却超出了byte的表示范围,则要存放在int类型变量里面。

    语句8表面上是int---->byte但是i1是一个int型的变量,它表示(-65535----65535)范围的整数,不能确定它的值,因此需要强制类型转换。语句4中128超出了byte的表示范围,因此不能赋值给byte变量b3需要进行强制类型转换,如语句5。

    例如语句5执行的结果是:-128,这是为什么呢?

int类型是32-bit,128的二进制表示形式为:00000000,00000000,00000000,10000000。Byte类型是8-bit,要把int类型强制转换成byte类型,则只截取其后8-bit,把前24-bit去除掉。因此byte b3=(byte)128;执行后的结果的二进制表示形式为:10000000,对应的整数即为-128。

⑥ 所有小于Int的整型运算,先转成int 再运算,因此语句13出现错误,需要进行强制类型转换,如语句14。

1 public class Float {
 2     public static void main(String[] args) {
 3         double d1=123.0;
 4         double d2=123.0F;
 5         //编译错误    Default data type is double
 6         //float f1=123.0; 
 7         //需要强制类型转换
 8         float f1=(float)123.0;
 9         
10         //int和long可以自动转换为float和double
11         float f2=123;
12         float f3=123L;
13         double d3=123;
14         double d4=123L;
15     }
16 }

Float Point Data Type-float and double

① Default data type is double
② A float data is 32-bit long
③ A double data is 64-bit long
④ A float data ends with 'f' or 'F'
⑤ A double data can end with 'd' or 'D'
在计算机领域对于float和double只能表示他们的近似值。因为小数可以无限,但是计算机表示的精度是有限的。
⑥ int和long可以自动转换为float和double

   Java变量类型:

boolean,byte,char,shot,int,long,float,double

  ② 引用类型:所有类、接口、数组、枚举、标注  

  Recommended Naming Rules

Class names begin with uppercase character
Interface names begin with uppercase character
Method names begin with lowercase character
Variable names begin with lowercase character
Constant variable names are in uppercase character,不同单词间用“_” 隔开,例如:USER_NAME。
Package names are in lowercase character

  规范我们可以违背,但是我们提倡遵守,养成一种良好的习惯! 

4、Operators  

  Assignment operators

  =   +=   -=   %=   *=   /=    <<=     >>=     >>>=    &=    ^=      |=

1 public class ByteDemo {
2     public static void main(String[] args) {
3         byte b1=2;
4         byte b2=3;
5         b1=(byte)(b1+b2);  // 语句1   加法,转int
6         b1+=b2;            //语句2    赋值,不转int
7     }
8 }

  b1+=b2;是否和语句b1=b1+b2完全等价?

我的理解:我们可以把“b11+=b2;”看做是对“b1=(byte)(b1+b2);”的优化!)

Comparison operators
  >    >=     <     <=     instanceof
  Equality operators
  = =        !=
  Arithmetic operators
  +       -       *       /        %
  Shift operators
  >>   <<    >>>
  Bitwise operators

(按位异或)   ~(按位取反)

  Logic operators

(逻辑与) &      ||  (逻辑或)  |      !(逻辑非)

和&都是逻辑与,它们之间的区别:&&是短路逻辑与,有短路的效果,效率更高。

和|都是逻辑或,它们之间的区别:||是短路逻辑或,有短路的效果,效率更高。

  Conditional  operators

  ? :

  Other operators

  ++    --

1 public class Test5 {
 2     public static void main(String [] args){
 3         int i=2;
 4         System.out.println(i++);   //2
 5         System.out.println(i);      //3
 6         int a=i++ + ++i;    //3+5=8
 7         System.out.println(a);
 8         System.out.println(i);  //5
 9         for(int j=0;j<1000;j++){
10             i=i++;
11         }
12         System.out.println(i);   //5
13     }
14 }

  程序分析:

  为什么经过1000次循环后,i=i++;的结果仍然是5?

  (老师讲解)++的运算符优先级高于=(赋值)运算符,i++的运算结果为5,进入寄存器,这时寄存器中存放的值为5。然后i的值变为6,继续执行赋值语句,赋值时,把寄存器中的值赋5值给i,因此i的值是5和6之间来回切换的!

我的理解)++的运算符优先级高于=(赋值)运算符,因此这个表达式的执行顺序如下:①运算i++这个表达式的结果为5 。----> ②i的值自加1(i++先参与运算,再自加1),即i的值变为6。----> ③将i++这个表达式的值,即第①部的结果赋值给变量i。("="表达式是将右边表达式的值赋值与左边的变量,因此是将5赋于i,而不是将6赋于i)。整个循环过程i的值在5和6之间来回切换。