Java自动化测试(数组/运算符)

引用类型

引用类型的变量它就是指的一个内存地址,一个位置信息

引用类型分类

  • 数组

  • 接口

  • 枚举

package com.zhongxin.variable;

public class RefVariable {
    public static void main(String[] args) {
        String s1 = "中文";
        System.out.println(s1);
        s1 = s1 + "英文";
        System.out.println(s1);
    }
}

数组

数组是一种数据结构,它是一个容器,用来盛放数据。不同类型的数组可用盛放不同类型的数据。

特点

  1. 容量确定,定义的时候必须指定数组的大小

  2. 类型确定,定义的时候必须声明数组的类型

分类

  1. 一维数组

  2. 多维数组

创建一个数组

创建一个数组,但是没有初始化数组的元素。这种情况需要显式的指定数组大小。

数据类型[ ] 数组名 = new 数据类型[size];

int[] myArr = new int[3];

创建一个数组,并已知数组的内部元素

类型[] 数组名 = {...};

int[] myArr = {0,1,2};
package com.zhongxin.variable;

public class ArrayDemo {
    public static void main(String[] args) {
        /*
         * 定义语法1
         * 数据类型[ ] 数组名 = new 数据类型[size];
         * 定义语法2
         * 数据类型[ ] 数组名 = {数据1,数据2,数据3};
         **/
        int[] myArr = new int[3];
        System.out.println(myArr[2]);

        int[] myArr1 = {100, 200, 300};
        System.out.println(myArr1[1]);
        // 数组长度
        // 最大索引 = 数组长度 - 1
        System.out.println(myArr.length);
        System.out.println(myArr1.length);

    }
}

例子:

char[] a = {'a', 'b', 'c'};
int[] b = {1, 2, 3};
long[] c = {4, 5, 6};
float[] d = {1.1f, 1.2f, 1.3f};
double[] e = {2.1, 2.2, 3.3};
boolean[] f = {true, false, false};
String[] h = {"Hello", "World"};

二维数组

二维数组定义语法1

数据类型[][] 数组名 = {{...}, {...}, {...}};

int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

二维数组定义语法2

数据类型[][] 数组名 = 数据类型 [二维数组长度][二维数组中一维数组的长度];

int[][] arr2 = new int[2][2];//{{0,0},{0,0}}

越界

System.out.println(arr2[0][100]);
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at com.zhongxin.variable.ArrayDemo2.main(ArrayDemo2.java:18)
package com.zhongxin.variable;

public class ArrayDemo2 {
    public static void main(String[] args) {
        /*
         * 二维数组定义语法1
         * 数据类型[][] 数组名 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
         * 二维数组定义语法2
         * 数据类型[][] 数组名 = 数据类型 [二维数组长度][二维数组中一维数组的长度];
         * */
        int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println(arr[0][0]);
        int[] arr1 = arr[0];
        System.out.println(arr1[0]);

        int[][] arr2 = new int[2][2];//{{0,0},{0,0}}
        System.out.println(arr2[0][0]);
        /*
            System.out.println(arr2[0][100]);
            越界
            Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
            at com.zhongxin.variable.ArrayDemo2.main(ArrayDemo2.java:18)
        */
    }
}

运算符

  • 算术运算符

  • 关系运算符

  • 逻辑运算符

  • 赋值运算符

  • 其他运算符

算术运算符

整数类型和整数类型运算只能得到整数类型

System.out.println(10 / 3);//3
System.out.println(10.0 / 3);//3.3333333333333335

% 取模 取余
前面的数比后面的数小 那么就返回前面的值

System.out.println(10 % 5); // 0
System.out.println(5 % 10); // 5 前面的数比后面的数小 那么就返回前面的值

自增 自减

int i = 1;
i++;
System.out.println(i);//2
i--;
System.out.println(i);//1
  • 参与运算 ++在后面 先把变量的值取出来赋值(赋值运算)再自增

  • 参与运算 ++在前面 先把变量的值自增1,再把值取出来赋值

int x = i++;
int y = ++i;
System.out.println(x); // 1
System.out.println(i); // 2
System.out.println(y); // 3
System.out.println(i); // 3
package com.zhongxin.operator;

public class OperatorDemo {
    public static void main(String[] args) {
        //算术运算符
        int a = 10;
        int b = 5;
        int c = a + b;
        System.out.println(a + b);
        System.out.println(c);
        // 整数类型和整数类型运算只能得到整数类型
        System.out.println(10 / 3);//3
        System.out.println(10.0 / 3);//3.3333333333333335
        // % 取模 取余
        System.out.println(10 % 5); // 0
        System.out.println(5 % 10); // 5 前面的数比后面的数小 那么就返回前面的值
        // ++ 自增
        // -- 自减
        int i = 1;
        i++;
        System.out.println(i);//2
        i--;
        System.out.println(i);//1

        //参与运算 ++在后面 先把变量的值取出来赋值(赋值运算)再自增
        int x = i++;
        //参与运算 ++在前面 先把变量的值自增1,再把值取出来赋值
        int y = ++i;
        System.out.println(x); // 1
        System.out.println(i); // 2
        System.out.println(y); // 3
        System.out.println(i); // 3

    }
}

关系运算符

关系运算符返回boolean类型

  • >

  • <

  • >=

  • <=

  • ==

  • !=

package com.zhongxin.operator;

public class OperatorDemo2 {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        System.out.println(a > b);
        System.out.println(a < b);
        System.out.println(a >= b);
        System.out.println(a <= b);
        System.out.println(a == b);
        System.out.println(a != b);
    }
}

逻辑运算符

  • &amp;

  • |

  • !

package com.zhongxin.operator;

public class OperatorDemo3 {
    public static void main(String[] args) {
        System.out.println('&');
        System.out.println(true & false); //false
        System.out.println(false & true); //false
        System.out.println(true & true); //true
        System.out.println(false & false); //false
        System.out.println('|');
        System.out.println(true | false); //true
        System.out.println(false | true); //true
        System.out.println(true | true); //true
        System.out.println(false | false); //false
        System.out.println('!');
        System.out.println(!false); //true
        System.out.println(!true); //false
    }
}

&& || 和& |最终结果是一样的 可以提高性能

造成短路,当逻辑运算符左边已经决定整个结果时,右边不执行

int a = 10;
int b = 5;
int c = 8;
System.out.println((a < b) & (a++ > c));
System.out.println(a); // 11
a = a - 1;
System.out.println((a < b) && (a++ > c));
System.out.println(a); //10

赋值运算符

赋值运算符=

扩展赋值运算符

  • -=

  • +=

  • *=

  • /=

package com.zhongxin.operator;

public class OperatorDemo4 {
    public static void main(String[] args) {
        // 把10赋值给a变量
        int a = 10;
        a += 10;// a = a + 10 左边和右边进行运算之后赋值给左边
        System.out.println(a);
    }
}

强制类型转换

大的变小的

double d = 3.14;
int x = (int) d;
System.out.println(x);

隐式类型转换

大的数据类型和小的数据类型运算时

System.out.println(10.0 / 3);    
package com.zhongxin.operator;

public class OperatorDemo4 {
    public static void main(String[] args) {
        // 把10赋值给a变量
        int a = 10;
        a += 10;// a = (int)(a + 10) 左边和右边进行运算之后赋值给左边
        System.out.println(a);

        // 强制类型转换 大的变小的
        double d = 3.14;
        int x = (int) d;
        System.out.println(x);
        // 隐式类型转换 大的数据类型和小的数据类型运算时
        System.out.println(10.0 / 3);
    }
}

三目运算符

又称三元运算符

a ? b : c;

a为真返回b否则返回c

package com.zhongxin.operator;

public class OperatorDemo5 {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int max = (a > b) ? a : b;
        System.out.println(max);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章