Java数据类型

数据类型

  • 强类型语言:要求变量的使用严格符合规定,所有变量都必须先定义后才使用。优点:安全性高,但速度较慢。
  • 弱类型语言:要求变量的使用不一定符合规定。如:VB、Javascript

java的数据类型分为两大类

  • 基本类型(primitive type):

    整数类型:

    • byte占1个字节,范围:-128-127
    • short占2个字节,范围:-32768-32767
    • int占4个字节,范围:-2147483648-2147483647
    • long占8个字节,范围:略
    //整数
    int num1 = 10;//最常用
    byte num2 =20;
    short num3 = 30;
    long num4 = 30L;//long类型要在数字后面加个L
    

    浮点类型:

    • float占4个字节
    • double占8个字节
    float num5 = 50.1F;//float类型要在数字后面加个F
    double num6 = 3.14;
    

    字符类型:

    • char占2个字节
    char name = 'A';//用单引号
    //字符串,String不是关键字,是类
    String name2 = "huangjilin";//用双引号
    

    boolean类型:

    • 占1个其值只有true和false两个
    boolean flag = true;
    
  • 引用类型(reference type):

    • 接口
    • 数组

拓展

//整数拓展:  二进制0b    十进制      八进制0      十六进制0x
        int i=10;
        int i1 = 010;//八进制0
        int i2 = 0xF;//十六进制0x   0~9 A~F
//浮点数拓展      有限    离散    舍入误差     接近但不等于
        // float double
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //银行业务怎么表示钱
        //BigDecimal  数学工具类

        float f = 0.1f;
        double d = 1.0/10;
        System.out.println(f==d);//false

        float d1 = 25252522525252525f;
        float d2 = d1 + 1;
        System.out.println(d1==d2);//true
 //字符拓展
        char c1 = 'a';
        char c2 = 'A';
        System.out.println(c1);
        System.out.println((int) c1);//强制转换
        System.out.println(c2);
        System.out.println((int) c2);//强制转换

        //所有的字符本质还是数字
        //编码   Unicode 表:会有字符对应的数字  2字节   0~65536
        // 表从U0000~UFFFF都有对应的表示字符

        char c3 = '\u0061';
        System.out.println(c3);//a
//常量,对象拓展		
		String sa = new String("hello,world");
        String sb = new String("hello,world");
        System.out.println(sa==sb);//false
        //两个为不同对象,储存在不同的内存地址中所以不相等

        String sc = "hello,world";
        String sd = "hello,world";
        System.out.println(sc==sd);//true
        //两者都为常量,均放在常量池中,所以相等
//布尔值拓展
        boolean flag = true;
        if (flag==true){}//新手
        if (flag){}//老手
        //Less is More!   代码要精简易读
posted @ 2023-03-09 22:00  远帆启航  阅读(31)  评论(0)    收藏  举报