数据类型

2022/9/12 23:24:34

本文主要是介绍数据类型,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

数据类型

强类型语言

  • 要求变量的使用严格规定,所有变量先定义再使用

  • //错误示范
    public class HelloWorld {
        public static void main(String[] args) {
            String a;
            System.out.println(a);
         }
    }
    ​
    //正确示范
    public class HelloWorld {
        public static void main(String[] args) {
            String a = "hello";
            System.out.println(a);
         }
    }

弱类型语言


Java的数据类型分为两大类

  • 基本类型(primitive type)

    1. 数值类型

    • 整数类型

      byte(1字节),short(2字节),int(4字节),long(8字节)

    • 浮点类型

      float(4字节),double(8字节)

    • 字符类型

      char

    1. boolean类型

    • 只有true和false(1位)

        //八大基本数据类型
        //整数
        int num1 = 10; //最常用
        byte num2 = 20;
        short num3 = 30;
        long num4 =30L; //Long类型要在数字加个L
​
        //小数:浮点数
        float num5 = 50.1F;
        double num6 = 3.1415926;
​
        //字符
        char name = '国';
        //字符串,String不是关键字,是类
        String name1 = "茶茶";
​
        //布尔值
        boolean flag = true;
        boolean flag1 = false;

什么是字节

  • 位(bit):计算机内部数据最小存储单位,11001100是一个八位二进制数

  • 字节(byte):是计算机中数据处理的基本单位,习惯用大写B来表示

  • 1B(byte,字节)=8bit(位)

  • 字符:指计算机中使用的字母、数字、字和符号

 

  • 1bit=1位

  • 1Byte表示一个字节1B=8b

  • 1024KB=1M

  • 1024KM=1G

  • 1024GB=1TB


  • 引用类型(reference type)

    • 接口

    • 数组

面试题

public class HelloWorld {
    public static void main(String[] args) {
        //整数拓展:  进制   二进制0b   十进制   八进制0   十六进制0x
        int i = 10;
        int i2 = 010;  //八进制0
        int i3 = 0x10;   //十六进制0x  0~9 A~F 16
​
        System.out.println(i);
        System.out.println(i2);
        System.out.println(i3);
​
        //=========================================================================================================
        //浮点数拓展?    银行业务怎么表示?钱
        //BigDecimal    数学工具类
        //=========================================================================================================
        //float   有限  离散   舍入误差    大约     接近但是不等于
        //double
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
        //最好完全避免使用浮点数进行比较
​
        float f = 0.1f;    //0.1
        double d = 1.0/10;   //0.1
        System.out.println(f==d);//false
​
        float d1 = 231313231313231313f;
        double d2 = d1+1;
        System.out.println(d1==d2);//true
​
​
        //=========================================================================================================
        //字符拓展?
        //================================================================================================
        char c1 ='a';
        char c2 = '中';
​
        System.out.println(c1);
        System.out.println((int)c1);  //强制转换
        System.out.println(c2);
        System.out.println((int)c2);
​
        //所有的字符本质还是涉及数字
        //编码 Unicode(占2个字节) 长度(0-65536) 表:(97=a 65=A)  Excel(长度:2的16次方=65536)
        //U0000 UFFF
        char c3 ='\u0061';
        System.out.println(c3);  //a
​
        //转义字符
        // \t   制表符
        // \n   换行
        System.out.println("hello\nworld");
​
        //
        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!  代码要精简易读
    }
}
​
​
​
//以上代码运行结果输出

10
8
16
false
true
a
97

20013
a
hello
world
false
true

 


这篇关于数据类型的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程