学习笔记 Java_毕向东_语言基础_程序流程控制 2014.7.29

来源:互联网 发布:微商和淘宝哪个东西贵 编辑:程序博客网 时间:2024/06/05 19:38

一、语言成分

1、类型转换 

  1.  byte b = 3; b = b + 2;  //错误:可能损失精度(2默认是int类型的)     
  2. 强制类型转换:b = (byte)(b + 2);   //得5。特别注意:byte要也要加个括号         
  3. 自动类型提升:'a' + 1  //得98。字符型的a会自动提升成int类型运算;(char)('a' + 1)   //得b;
  4. 问题:可是,为什么byte m = 5; System.out.println('a' + m);  //也得102呢?不是向上提升吗?m是byte类型(1个字节),char是两个字节呀;'a' +'b'  //为什么得195呢?谁给它们转的呀?
  5. char f = 97;  //得a;char g = 'a';  //得a,如果参与运算的话,就看做97
  6. 表达式的数据类型自动提升:所有的byte,short,char型的值将被提升到int型。short i = 4; byte j = 5; i = j + j; //错误:可能精度受损。要是i = j;就可以。为什么呀?short币byte多一个字节,还不够装呀?

2、算术运算符

  1. a+1;  //这不是语句,要这么写a=a+1;
  2. a++;  //--> a=a+1;
  3. 字符串数据和任何数据使用+都是相连接,最终都会变成字符串。 如:“5+5=”+5+5;  //得5+5=5 -->5+5=55

3、转义字符

  1. \n:换行。System.out.println("Hello \nworld");  //换行
  2. \b:退格。相当于backspace。如:System.out.println("Hello\bworld");  //得Hellworld
  3. \r:按下回车键?window中,回车符2时由两个字符来表示\r\n
  4. \t:制表符。相当于tab键
  5. char ch = '\'';  //如果char ch = ''';  这样定义就错了
  6. char c = '嘿';一个汉字默认是两个字节

4、赋值运算符

  1. short s = 4; s = s + 5;  //不行  这是两次运行,有个类型转换。s + 5是个int类型的值,再赋给s是装不下的,容易丢失精度
  2. s += 5;  //OK  这个是一次运算,只做赋值运算,它内部有个自动转换动作

5、逻辑运算符:

  1. &(And与)左边无论真假,右边都要运算  
  2. &&(And短路)如果左边为真,右边运算;如果左边为假,右边不运算。(| 和 || 同理)
  3. |(或)    ^(异或)
  4. !(Not非)!true得false

6、位运算符:

  1. 3 << 2 = 12;  // --> 3 * 2 * 2 = 12;6 >> 2  = 1;  // --> 6 / (2 * 2) = 1;>>> 最高位无论是0或1,都是补0
  2. ~(反码,取反)如:~6 = 7
  3. &(与运算)如:6 & 3 = 2              和逻辑运算符怎么区分呢?都长得一样。
  4. |(或运算)如:6 | 3 = 7 
  5. 一个数异或同一个数两次,结果还是那个数。如:7 ^ 4 ^ 4 = 7
  6. 最有效的方法算出2乘以8等于几:2 >> 3 = 8;(位运算高效)
  7. 对两个整型变量的值进行交换(不需要第三方变量)
    int m = 8, n = 3;/*11 = 8 + 3;3 = 11 - 8;8 = 11 -3;*/n = m + n;  //如果n和m的值非常大,就容易超出int范围m = n - m;n = n - m;System.out.println("m=" + m + "," + "n=" + n);
    //这就不涉及到超出范围。这是技巧型方式,一般人不会想到。实际开发中还是用temp那种方法,阅读性比较强。int x = 8, y = 3;x = x ^ y;y = x ^ y;  //即(x ^ y) ^ y:一个数异或同一个数两次,结果还是那个数x = y ^ x;  //即(x ^ y) ^ x;System.out.println("x=" + x + "," + "y=" + y);
  8. 60的16进制。代码:MyOperateDemo3.java
    class MyOperateDemo3 {public static void main(String[] args) {int num = 60;  //60 == 0x3Cint n1 = num & 15;  //得60的16进制的低四位。15的二进制为1111System.out.println((char)(n1 - 10 + 'A'));int n2 = num >>> 4;  //右移的时候没用>>。用>>> ,前面补0,才能移光  n2 = n2 & 15;  //得60的16进制的高四位//n2 = (n2 > 9) ? (char)(n2 - 10 + 'A') : n2;System.out.println(n2);}}

 

二、程序流程控制

1、if

  1. if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;
  2. 三元运算符:好处:可以简化if else代码;弊端:因为是一个运算符,所以运算完必须要有一个结果。
  3. int b = 3;if(b > 1)System.out.println("a");else if(b > 2)System.out.println("b");System.out.println("c");  //得a c
    int b = 3;if(b > 1)System.out.println("a");if(b > 2)System.out.println("b");System.out.println("c");  //得a b c
  4. 需求:根据用户指定月份,打印该月份所属的季节。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
    class MyIfTest {public static void main(String[] args) {int month = 48;if(month > 12 || month < 1)System.out.println(month + ":不是有效月份");else if(month > 2 && month < 6)System.out.println(month + "月:春季");else if(month > 5 && month < 9)System.out.println(month + "月:夏季");else if(month > 8 && month < 12)System.out.println(month + "月:秋季");else System.out.println(month + "月:冬季");}}

2、switch

  1. case之间与default没有顺序。先执行第一个case,没有匹配的cae执行default
  2. 需求:根据用户指定月份,打印该月份所属的季节。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
    class MySwitchTest {public static void main(String[] args) {int month = 3;switch(month){case 3:case 4:case 5:System.out.println(month + "月:春季");break;case 6:case 7:case 8:System.out.println(month + "月:夏季");break;case 9:case 10:case 11:System.out.println(month + "月:秋季");break;case 12:case 1:case 2:System.out.println(month + "月:冬季");break;}}}

3、while和for可以互换,区别在于:

  1. for为了循环而定义的变量在for循环结束时就在内存中释放(for(int x = 0; x < 3; x++) {})(变量只为循环的次数而存在,用for较内存要优化一点)
  2. 而while循环使用的变量在循环结束后还可以继续使用(int x = 0; while(x < 3) {})

4、无限循环的最简单表现形式

  1. for(;;){}
  2.  while(true){}

 

 

 

 

 

 

 

 


  

0 0