zoukankan      html  css  js  c++  java
  • JAVA基础总结2

    2.1 关键字

      常用的关键字主要包括如下:

      

    2.2 标识符

      简单而言标识符它其实就是用于标识某些东西的符号。

      

    2.3 注释的应用

      注释说明

      

      注释的作用:

      

      注释的应用:

      

    2.4 常量和变量

       常量的定义与分类

      

      进制的由来

      

       注意:byte 是1个字节 = 8个二进制位, 1K = 1024。

      进制的基本转换介绍

      

        

      负数的进制

      

      变量的介绍

       

      JAVA的数据类型主要包含引用类型和基本数据类型,其中有8种基本数据类型

      

      变量的基本演示

      

      类型提升与强制转换说明

      

      结果分别是97与98,其实就是二进制和生活中文字对应的关系表,即ASCII编码表,反之:System.out.println(char('a' + 1)), 输出结果等于b, 其中b对应的编码表里面的数字是98。

      类型运算细节

     1 class DemoTest20171015
     2 {
     3     public static void main(String[] args) 
     4     {
     5 //        int x = 3;
     6 //        byte b = 5;
     7 //        x = x + b;
     8 //        System.out.println("x=:" + x); 
     9 
    10 //        byte b = 3;
    11 //        b = (byte)(b + 200);
    12 //        System.out.println("b=:" + b); //输出结果为-53, 强制类型转换
    13 
    14 //        System.out.println('你' + 0); //输出结果为20320,即unicode国际编码表对应的关系
    15 
    16         byte b = 4;
    17 //        b = 3+7;
    18 //        System.out.println("b=:" + b); //输出结果为10
    19 //        byte b1 = 3;
    20 //        byte b2 = 7;
    21 //        b = b1 + b2;
    22 //        System.out.println("b=:" + b); //编译报错,说int转换byte可能损失精度,原因是b1和b2是变量,其中它们的值是变化的,无法判断;
    23         //而上面b = 3+7是属于固定的常量,可以判断,所以输出结果等于10.
    24 
    25         int x;
    26         int x1 = 10;
    27         int x2 = 98;
    28             x = x1 + x2;
    29         System.out.println("x=:" + x); //输出结果为108,因为x1和x2默认都是int类型,所以最终运算结果还是为整型,即int类型。
    30 
    31 
    32     }
    33 }

    2.5 运算符

       JAVA中主要包含以下6种运算符:

      

      算术运算符

      说明:算术运算符主要包含:+ - * /  %(取余,模运算) ++(自增:在原有数据基础之上+1,再赋给原有数据)  --

      

      练习: 

     1    int x = 6370;
     2  
     3     x = x / 1000 * 1000
     4  
     5    System.out.println(x); // 输出结果为6000
     6   
     7     System.out.println(5 % 2); //输出结果为1
     8   
     9    System.out.println(2 % 5); //输出结果为2
    10  
    11    System.out.println(3 % 5); //输出结果为3
    12  
    13    System.out.println(-5 % 2); //输出结果为-1
    14 
    15   System.out.println(5%-2); //输出结果为1
    16  
    17    System.out.println(5%5); //输出结果为0
    18  
    19     注意:任何数字和字符串相连接,其结果为字符串,如:System.out.println("ab" + 5 + 5); //输出结果为ab55
    20  
    21    int a = 3;
    22  
    23    a++; //a= a+1;
    24  
    25   System.out.println(a); //输出结果为4
    26  
    27   int a =3,b;
    28 
    29     b = a++;
    30  
    31    System.out.println("a=" + a + ", b=" + b); //输出结果a=4, b=3
    32  
    33    int a =3,b;
    34  
    35    b = ++a;
    36  
    37    System.out.println("a=" + a + ", b=" + b); //输出结果a=4, b=4
    38  
    39    int i = 3;
    40  
    41    i = i++;
    42  
    43    System.out.println(i) ;  //输出结果为3

      

      赋值运算符

      说明:主要包含: =  +=  -=  /=  *=  %=

    1  int a = 4;
    2 
    3   a+=2;//a = a + 2;
    4 
    5   short s = 3;
    6 
    7   s+= 4;//输出结果为7
    8 
    9   // s = s+4; //运行编译报错,有可能损失精度,原因是没有做强转,而s+=4可以自动做转换

      比较运算符

      

      逻辑运算符

       

       思考:逻辑运算符有什么用呢?

      用于连接两个boolean类型的表达式,如:2<x < 5, x > 2 & x < 5;

      &符号的运算特点:

      true & true = true;

      true & false = false;

      false & true = false;

      false & false = false;

      &符号的运算规律:

      &运算符两边只有一个是false,结果肯定是false;只有两边都是true,结果才是true

      |符号的运算特点:

      true | true = true

      false | true = true

      true | false  = true

      false  | false = false

      |符号的运算规律

      |运算符 的两边只要有一个是true,结果肯定是true;只有两边都是false,结果才是false

      

      ^:异或符号的运算特点:

      true ^ true = false

      false ^ true = true

      true ^ false  = true

      false  ^ false = false

      

      ^符号的运算规律:

      ^符号的两边结果如果相同,结果是false;两边的结果不同,结果是true。

      !:非运算符,判断事物的另一面

      !true = false;

      !false  = true;

      !!true = true;

      &&符号的运算特点:

      和&运算的结果是一样的,但是运算过程有点小区别。

      &符号无论左边的运算结果是什么,右边都参与运算;&&当左边为false时,右边不参与运算。  

      ||符号的运算特点:

      和|运算结果是一样的,但是运算过程有点小区别。

      |符号无论左边的运算结果是什么,右边都参与运算;||当左边是true时,右边不参与运算。

      位运算符

      

      << 相当于乘以2的倍数

      >> 相当于除以2的倍数

      移n位,就是对乘以或者除以2的n次幂。

       图解

      

      

         

      三元运算符

      定义:简单而言就是三个元素参与运算的符号。

      

      演示:

     1 class OperateDemo
     2 {
     3         public static void main(String args[])
     4         {
     5             int x = 0, y;
     6             y = (x > 1)?100:200;
     7             System.out.println(y); // 输出结果为200
     8                 
     9             //获取两个整数中的较大的整数
    10             int a,b;
    11             int max = (a>b)?a:b;
    12 
    13             //获取三个整数中的较大的整数
    14             int o,p,q;
    15             int temp = (o>p)?o:p;
    16             int max2 = (temp>q)?temp:q;
    17         }
    18 
    19 }

      练习:

      

      1、 直接操作二进制位,或使用移位运算(2<<3)

      

      2、

     1 class OperateTest 
     2 {
     3     public static void main(String[] args) 
     4     {
     5         //最有效率的方式算出2乘以8等于几
     6         System.out.println(2<<3);
     7 
     8         //对两个整型变量的值进行互换(不需要第三方变量)
     9         int a = 3, b = 5;
    10         System.out.println("a="+a+",b="+b);
    11         
    12         //开发时使用,使用第三方变量的形式,因为阅读性强
    13 //        int c;
    14 //        c = a;
    15 //        a = b;
    16 //        b = c;
    17 //        System.out.println("a="+a+",b="+b);
    18 
    19 
    20         //这种方式最好不要用,如果两个整数的数值过大,会超出int范围,会强制转换,数值会变化。
    21 //        a = a+b;//a = 3+5 = 8;
    22 //        b = a-b;//b = 3+5-5 = 3;
    23 //        a = a-b;//a = 3+5-3 = 5;
    24 //        System.out.println("a="+a+",b="+b);
    25 
    26         //面试时使用
    27         a = a^b;//a = 3^5;
    28         b = a^b;//b = (3^5)^5; b=3;
    29         a = a^b;//a = (3^5)^3; a=5;
    30         System.out.println("a="+a+",b="+b);
    31 
    32     }
    33 }

    2.6 语句

       流程控制语句

      2.6.1 判断结构

        if语句三种格式:

        1. if (条件表达式)

        {
          执行语句;  
        }
        2. if (条件表达式)
        {
          执行语句;  
        }
        else
        {
          执行语句;
        }
        3.if (条件表达式)
        {
          执行语句;  
        }
        else if (条件表达式)
        {
          执行语句;
        }
        ......
        else 
        {
          执行语句;
        }

      if 语句练习-星期

       int week = 1;
      if (week == 1)
      {
        System.out.println(week + "对应的中文是星期一");
      }
      else if (week == 2)
      {
        System.out.println(week + "对应的中文是星期二");
      }
      else if (week == 3)
      {
        System.out.println(week + "对应的中文是星期三");
      }
      else if (week == 4)
      {
        System.out.println(week + "对应的中文是星期四");
      }
      else if (week == 5)
      {
        System.out.println(week + "对应的中文是星期五");
      }
      else if (week == 6)
      {
        System.out.println(week + "对应的中文是星期六");
      }
      else if (week == 7)
      {
        System.out.println(week + "对应的中文是星期三日");
      }
      else 
      {
        System.out.println("没有对应的星期");
      }
    

     if 语句练习-季节

      

     1 int month = 13;
     2 if (month <1 || month >12)
     3 {
     4        System.out.println("月没有对应的季节");              
     5 }
     6 else if (month>=3 && month <= 5)
     7 {
     8        System.out.println("月是春季");
     9 }   
    10 else if (month>=6 && month <= 8)
    11 {
    12        System.out.println("月是夏季");
    13 } 
    14 else if (month>=9 && month <= 11)
    15 {
    16        System.out.println("月是秋季");
    17 } 

    2.6.2 Switch语句

      

     

      

    2.7 函数

     定义

      

      特点

       

      应用

      

      重载

      

      

    2.8 数组

  • 相关阅读:
    模块总结
    安装python包时出现VC++ 错误的解决方案
    Android之drawable state各个属性详解
    【Android 复习】:第01期:引导界面(一)ViewPager介绍和使用详解
    Android 应用页面延缓载入
    Android系统手机端抓包方法
    【Android 复习】:Android之ViewFlipper(二)
    【Android 复习】:Android之ViewFlipper(一)
    【Android 复习】:Android五种布局的使用方法
    【Android 复习】:从Activity中返回数据
  • 原文地址:https://www.cnblogs.com/lingf/p/7671023.html
Copyright © 2011-2022 走看看