• Java基础03-数据类型


    数据类型

    一、八大基本数据类型
    1. 整数型-int

      /*
      1 int 数据类型是32位、有符号的以二进制补码表示的整数;
      2 最小值是 -2,147,483,648(-2^31);
      3 最大值是 2,147,483,647(2^31 - 1);
      4 一般地整型变量默认为 int 类型;
      5 默认值是 0 ;
      */
      int num1 = 10;  //最常用
    1. 整数型-byte

      /*
      1 byte 数据类型是8位、有符号的,以二进制补码表示的整数;
      2 最小值是 -128(-2^7);
      3 最大值是 127(2^7-1);
      4 默认值是 0;
      5 byte 类型用在大型数组中节约空间,主要代替整数,因为 byte 变量占用的空间只有 int 类型的四分之一;
      */
      byte num2 = 20;
    2. 整数型-short

      /*
      1 short 数据类型是 16 位、有符号的以二进制补码表示的整数
      2 最小值是 -32768(-2^15);
      3 最大值是 32767(2^15 - 1);
      4 Short 数据类型也可以像 byte 那样节省空间。一个short变量是int型变量所占空间的二分之一;
      5 默认值是 0;
      */
      short num3 = 30;
    3. 整数型-long

      /*
      1 long 数据类型是 64 位、有符号的以二进制补码表示的整数;
      2 最小值是 -9,223,372,036,854,775,808(-2^63);
      3 最大值是 9,223,372,036,854,775,807(2^63 -1);
      4 这种类型主要使用在需要比较大整数的系统上;
      5 默认值是 0L;
      */
      long num4 = 30L;    //Long类型要在数字后面加个L
    4. 浮点型-float

      /*
      1 float 数据类型是单精度、32位、符合IEEE 754标准的浮点数;
      2 float 在储存大型浮点数组的时候可节省内存空间;
      3 默认值是 0.0f;
      4 浮点数不能用来表示精确的值,如货币;
      */
      float num5 = 50.1F; // Float类型要在数字后面加F
    5. 浮点型-double

      /*
      1 double 数据类型是双精度、64 位、符合IEEE 754标准的浮点数;
      2 浮点数的默认类型为double类型;
      3 double类型同样不能表示精确的值,如货币;
      4 默认值是 0.0d;
      */
      double num6 = 3.1415926;
    6. 字符型-char&String

      /*
      char类型是一个单一的 16 位 Unicode 字符;
      最小值是 u0000(即为0);
      最大值是 uffff(即为65,535);
      char 数据类型可以储存任何字符;
      */
      char name1 = '中';   //只能写一个字
      char name2 = '国';
      char name3 = 'A';
      ​
      // 字符串,String不是关键字,他是一个类
      String namea = "lich";
    7. 布尔型

      /*
      boolean数据类型表示一位的信息;
      只有两个取值:true 和 false;
      这种类型只作为一种标志来记录 true/false 情况;
      默认值是 false;
      */
      boolean one = true;

       

    二、整数拓展知识
    int i1 = 10;    //十进制
    int i2 = 010;   //八进制以0开头通途
    int i3 = 0x101;  //十六进制以0x开头
    ​
    System.out.println(i1);
    System.out.println(i2);
    System.out.println(i3); //0x101转化为十进制 = 257
                            // 方法:16^2*1+16^1*0+16^0*0 = 256+0+1 = 257
    三、浮点数拓展知识
    // 浮点数拓展:float 和 double
    // float 有限的,离散的,舍入误差,大约,接近但不等于
    // 最好完全使用浮点数进行比较
    // 最好完全使用浮点数进行比较
    // 最好完全使用浮点数进行比较
    // =======================================
    // BigDecimal 数学工具类,银行业务用这个类来表示
    // =======================================
    float f = 0.1f;     //0.1
    double d = 1.0/10;  //0.1
    ​
    System.out.println(f);
    System.out.println(d);
    System.out.println(f==d);
    四、字符拓展知识
    char c1 = 'a';
    char c2 = '中';
    char c3 = 'u0061';
    ​
    System.out.println(c1);
    System.out.println((int)c1);    //强制转换为数字类型
    ​
    System.out.println(c2);
    System.out.println((int)c2);     //强制转换为数字类型
    ​
    System.out.println(c3); // 与打印c1得到同样的结果,就是查了unicode编码表
    // 所有的字符本质还是数字
    // 查编码unicode表: 97=a, 20013=中 , 2个字节,可以0~65536
    五、转译字符
    //转义字符
    // 	 制表符
    // 
     换行
    System.out.println("hell	world");  //中间多了一个tab的空格
    System.out.println("hell
    world");  //两个单词就被换行了
    ​
    String sa = new String("com.lich.base.hello world");
    String sb = new String("com.lich.base.hello world");
    System.out.println(sa==sb); //不相等
    ​
    String sc = "com.lich.base.hello world";
    String sd = "com.lich.base.hello world";
    System.out.println(sc==sd); //相等
    六、类型转换
    // 强制转换 -> (类型)变量名 高-低
    int i =128;
    byte b = (byte) i;  //内存溢出问题,byte对应Byte类,里面整数最大值为127
    System.out.println(i);  //128
    System.out.println(b);  //-128
    ​
    ​
    // 自动转换 低-高
    int j =128;
    double d = j;  
    ​
    System.out.println(j);  //128
    System.out.println(d);  //128.0
    /*
    1 不能对布尔值进行转换
    2 不能把对象类型转换为不想干的类型
    3 在把高容量转换到低容量的时候,强制转换
    4 转换的时候可能存在内存溢出,或者精度问题提
    */
    ​
    System.out.println((int)23.7);      //23
    System.out.println((int)-45.89f);   //-45
    ​
    ​
    char c = 'a';
    int e = c+1;
    System.out.println(e);      //98
    System.out.println((char)e);//b

     

  • 相关阅读:
    Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程
    Xamarin开发手机聊天程序
    线上处理数据转载
    Logstash
    Webpack按需加载一切皆模块
    网络编程
    NET Core 事件总线
    Using INSERT IGNORE with MySQL to prevent duplicate key errors
    Spring的两种任务调度Scheduled和Async
    Embedded servlet containers
  • 原文地址:https://www.cnblogs.com/lich1x/p/12984805.html
Copyright © 2020-2023  润新知