变量

一、基本数据类型

  • boolean(布尔值):
    boolean bool1 = true;
    boolean bool2 = false;
    System.out.println(bool1);
    System.out.println(bool2);
    bool1 = false; //变量重赋值
    System.out.println(bool1);
  • byte(整数):
    byte b1 = 100;
    byte b2 = 120;
    System.out.println(b1);
    System.out.println(b2);

  • short:
    short s2 = 10000;
    int i1 = 10;
    int i2 = 0;
    long l1 = 1;
    //long l2 = 922337303685477; java中默认是int,超出int范围报错
    //long l3 = 922337203685477l; java中加L,表示是Long类型,不报错
    long l4 = 9223372036854778l;
    System.out.println(b1);
    System.out.println(b2);
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(i1);
    System.out.println(i2);
    System.out.println(l4);
  • int-Integer:
  • long
  • float(浮点数、小数):
    //float fa = 3.14; // 此操作报错,java中输入小数默认是double类型,不可以赋值给float,double范围比float大,java中不允许大值赋值给小的数据类型,需要加f或者F
    float f2 = 3.14f;
    float f3 = 3.14F;
    System.out.println(f2);

  • double:
    double f4 = 3.145D;
    double f5 = 3.14; // double一般不加
    System.out.println(f5);
  • char-Character

二、引用数据类型

引用数据类型:引用数据类型是地址值,基本数据类型是具体的值

1、String(字符串):

//单引号 是 字符类型 char  'a'
//双引号 是 字符串类型 String
String str1 = "java 学习";
System.out.println(str1);
str1 = "";
System.out.println(str1);

 

 

2、一维数组:

数组描述一组具有相同意义的数据类型
(1)动态和静态初始化、length、取值、赋值、索引越界
int[] arry = {1, 2, 3};
System.out.println("arry数组的地址值为:" + arry);
System.out.println("arry数组的长度为:" + arry.length);
System.out.println("索引为0的数据为:" + arry[0]);
arry[1] = 100;
System.out.println("重新赋值后的arry[1]:" + arry[1]);

int[] arry2 = new int[5];
System.out.println("动态创建数组:" + arry2);
System.out.println("动态创建数组arry2[0]值为:" + arry2[0]);
arry2[4] = 1000;
System.out.println("赋值后arry2[4]的值为:" + arry2[4]);
System.out.println("下标索引越界:" + arry2[5]);

 

 

 

 

(2)空指针异常:NullPointerException,访问了为null的数据类型的内容

int[] arry3 = null;
System.out.println("空指针异常:" + arry3[0]);

3、二位数组

1. 静态初始化:数据类型[][] 数组名 = {{值1,值2,},{值1,值2},{值1,值2}};
String[][] zhubos = {
{"李佳琪", "薇娅", "罗杰"},
{"罗永浩", "金星"},
{"张三", "李四", "王二麻子"}};
System.out.println(zhubos);
System.out.println(zhubos[0]);
System.out.println(zhubos[0][2]); // 罗杰
zhubos[0][2] = "海贼王";
System.out.println(zhubos[0][2]); // 海贼王
System.out.println(zhubos.length); // 6

 

 

// 2. 动态初始化:数据类型[][] 数组名 = new 数据类型[二位数组的长度][];
String[][] zhubos2 = new String[3][];
System.out.println(zhubos2);
System.out.println(zhubos2[0]);
zhubos2[0] = new String[3];
zhubos2[0][0] = "A1";
zhubos2[0][1] = "A2";
zhubos2[0][2] = "A3";
System.out.println(zhubos2[0][0]);
System.out.println(zhubos2[0][1]);
System.out.println(zhubos2[0][2]);


zhubos2[1] = new String[2];
zhubos2[1][0] = "B1";
zhubos2[1][1] = "B2";
System.out.println(zhubos2[1][0]);
System.out.println(zhubos2[1][1]);


zhubos2[2] = new String[3];
zhubos2[2][0] = "A21";
zhubos2[2][1] = "A22";
zhubos2[2][2] = "A23";
System.out.println(zhubos2[2][0]);
System.out.println(zhubos2[2][1]);
System.out.println(zhubos2[2][2]);

 

posted @ 2022-09-10 13:13  袁丫头  阅读(61)  评论(0)    收藏  举报