骑士李四记录:

Java数据类型(type)可以分为两大类:

基本类型(primitive types)和引用类型(reference types)

java8大基本数据类型

byte:8位,最大存储数据量是255,存放的数据范围是-128~127之间。
short:16位,最大数据存储量是65536,数据范围是-32768~32767之间。
int:32位,最大数据存储容量是2的32次方减1,数据范围是负的2的31次方到正的2的31次方减1。
long:64位,最大数据存储容量是2的64次方减1,数据范围为负的2的63次方到正的2的63次方减1。
float:32位,数据范围在3.4e-45~1.4e38,直接赋值时必须在数字后加上f或F。
double:64位,数据范围在4.9e-324~1.8e308,赋值时可以加d或D也可以不加。
boolean:只有true和false两个取值。
char:16位,存储Unicode码,用单引号赋值。

数据类型之间的转换

* 1.自动类型转化:从小类型到大类型转化:
     * byte->short->int->long->float->double
     * 2.强值类型转化:从大类型到小类型转化:
     * double->float->long->int->short->byte  会出现溢出,精度丢失
     * char -> int
     * 两点规则:
     * 1.整数直接量可以直接赋值给byte,short,char,但是不可以超范围
     * 2.byte,short,char参与运算时,先一律转为int
     *

String和数据类型int,char中间的转化

可以参考骑士李四之前的记录:
String转int,charint转String:

练习代码:

package com.lipeng.programdemo.javabase;

/**
 * @Description:
 * @Author: pengli
 * @CreateDate: 2020/5/16 12:33
 */
public class TestDataType {
    public static void main(String args[]) {
        /**
            变量的使用:byte, short,int,long,double,float,char,boolean
            1.必须数据类型匹配,
            2.java 中规定在申明变量同时要使用必须初始化,否则编译错误。
            3.运算时候防止溢出,数据超出范围,就会溢出。
            4.运算时候小的类型会自动转成大类型
            5.通过时间毫秒来存储时间  System.currentTimeMillis();
         */
        //byte   1个字节8位,
        //short  2个字节16位,
        //int    4个字节32位,-21亿->21亿
        int a=0 ,b =10;
        int c = a+b;
        System.out.println(c);

        //long 8个字节64 Long直接量加l
        long lon = 10000000;
        Long f = 100000000l;
        //通过时间毫秒来存储时间
        Long time = System.currentTimeMillis();
        System.out.println(time);


        //float  4个字节32位
        float g = 200.1f;

        //Double 8个字节64位 运算有舍入误差,浮点数默认是double,表示float加f
        double d = 0.1;
        double e = 0.1/b;
        System.out.println(e);

        // char 2个字节16位,单引号表示,实质上就是Unicode码, 赋值三种方式
        // 1.字符直接量,表示存储的是该字符的Unicode编码
        // 2.整型直接量0-65535,表示的也是对应的Unicode
        // 3.Unicode形式,'\u0041',Unicode字符是16进制形式。
        char c1 = '中';
        char c2 = 65;
        char c3 = '\u0041';
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        //单引号加值,就是表示unicode码
        System.out.println('2'+'2');

        //boolean 1个字节 8位

        /**
         * 数据类型转化:
         * 1.自动类型转化:从小类型到大类型转化:
         * byte->short->int->long->float->double
         * 2.强值类型转化:从大类型到小类型转化:
         * double->float->long->int->short->byte  会出现溢出,精度丢失
         * char -> int
         * 两点规则:
         * 1.整数直接量可以直接赋值给byte,short,char,但是不可以超范围
         * 2.byte,short,char参与运算时,先一律转为int
         */
        int aa = 5;
        long bb = aa; //int->long 自动转
        int cc = (int) bb; //long->int 强制换
        long dd = 5; //这是自动类型int 直接转long

        long ll = 10000000l*1024*1024*1024;
        int  ii = (int) ll;
        System.out.println("溢出"+ii);// 会出现溢出

        double hh =25.9877;
        int i = (int) hh;
        System.out.println("精度丢失"+i);//  精度丢失

        char ch = 'A';
        int  in = ch;
        int  in1 = 9;
        char ch1 = (char) (in1+'0');
        System.out.println("char----"+ch);
        System.out.println("char转int---"+in);
        System.out.println("int转char---"+ch1);//

        byte b1 = 5;
        byte b2 = 6;
        byte b3 = (byte) (b1+b2);  //参与运算时,先一律转为int运算完,再强转byte,赋值b3
        System.out.println(b3);

        char char1 ='2';
        char char2 = '2';
        char char3 = (char) (char1 + char2);
        char char4 =100;
        char char5 =50;
        System.out.println(char1);
        System.out.println(char1+char2);
        System.out.println(char3);
        System.out.print(char4);
        System.out.println(char5);

    } 
}