Java数据类型

数据类型 每一种数据都定义了明确的数据类型,在内存中分配了不同大小的内存空间(字节)。

  1. java 数据类型分为两大类
    基本数据类型,引用类型

  2. 基本数据类型有 8 种
    数值型 [byte , short , int , long , float ,double] char , boolean

  3. 引用类型 [类,接口, 数组]

整数类型

基本介绍

Java 的整数类型就是用于存放整数值的,比如 12 , 30, 3456 等等

案例演示:

byte n1 = 10;
short n2 = 10;
int n3 = 10;//4 个字节
long n4 = 10;//8 个字节

整型的类型

整型的使用细节 IntDetail.java

public class IntDetail {
//编写一个 main 方法
    public static void main(String[] args) {
//Java 的整型常量(具体值)默认为 int 型,声明 long 型常量须后加‘l’或‘L’
        int n1 = 1;//4 个字节
//int n2 = 1L;//对不对?不对
        long n3 = 1L;//对
    }
}

浮点类型

基本介绍

Java 的浮点类型可以表示一个小数,比如 123.4 ,7.8 ,0.12 等等

浮点型的分类

说明一下

  1. 关于浮点数在机器中存放形式的简单说明,
    浮点数=符号位+指数位+尾数位

  2. 尾数部分可能丢失,造成精度损失(小数都是近似值)

浮点型使用细节 FloatDetail.java

public class FloatDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
//Java 的浮点型常量(具体值)默认为 double 型,声明 float 型常量,须后加‘f’或‘F' //float num1 = 1.1; //对不对?错误
        float num2 = 1.1F; //对的
        double num3 = 1.1; //对
        double num4 = 1.1f; //对
//十进制数形式:如:5.12 512.0f .512 (必须有小数点)
        double num5 = .123; //等价 0.123
        System.out.println(num5);
//科学计数法形式:如:5.12e2 [5.12 * 10 的 2 次方 ] 5.12E-2 []
        System.out.println(5.12e2);//512.0
        System.out.println(5.12E-2);//0.0512
//通常情况下,应该使用 double 型,因为它比 float 型更精确。
//[举例说明]double num9 = 2.1234567851;float num10 = 2.1234567851F;
        double num9 = 2.1234567851;
        float num10 = 2.1234567851F;
        System.out.println(num9);
        System.out.println(num10);
//浮点数使用陷阱: 2.7 和 8.1 / 3 比较
//看看一段代码
        double num11 = 2.7;
        double num12 = 2.7; //8.1 / 3; //2.7
        System.out.println(num11);//2.7
        System.out.println(num12);//接近 2.7 的一个小数,而不是 2.7
//得到一个重要的使用点: 当我们对运算结果是小数的进行相等判断是,要小心//应该是以两个数的差值的绝对值,在某个精度范围类判断
        if (num11 == num12) {
            System.out.println("num11 == num12 相等");
        }
//正确的写法 , ctrl + / 注释快捷键, 再次输入就取消注释
        if (Math.abs(num11 - num12) < 0.000001) {
            System.out.println("差值非常小,到我的规定精度,认为相等...");
        }
// 可以通过 java API 来看 下一个视频介绍如何使用 API
        System.out.println(Math.abs(num11 - num12));
//细节:如果是直接查询得的的小数或者直接赋值,是可以判断相等
    }
}

字符类型(char)

基本介绍

字符类型可以表示单个字符,字符类型是 char,char 是两个字节(可以存放汉字),多个字符我们用字符串 String(我们 后面详细讲解 String)

案例演示 Char01.java

#代码
char c1 = 'a';
char c2 = '\t';
char c3 = '韩';
char c4 = 97;

字符类型使用细节

public class CharDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
       //在 java 中,char 的本质是一个整数,在默认输出时,是 unicode 码对应的字符//要输出对应的数字,可以(int)字符
        char c1 = 97;
        System.out.println(c1); // a
        char c2 = 'a'; //输出'a' 对应的 数字
        System.out.println((int) c2);
        char c3 = '韩';
        System.out.println((int) c3);//38889
        char c4 = 38889;
        System.out.println(c4);//韩
        //char 类型是可以进行运算的,相当于一个整数,因为它都对应有 Unicode 码. System.out.println('a' + 10);//107
        //课堂小测试
        char c5 = 'b' + 1;//98+1==> 99
        System.out.println((int) c5); //99
        System.out.println(c5); //99->对应的字符->编码表 ASCII(规定好的)=>c
    }
}

布尔类型:boolean

public class Boolean01 {
    //编写一个 main 方法
    public static void main(String[] args) {
        //演示判断成绩是否通过的案例
        //定义一个布尔变量
        boolean isPass = true;//
        if (isPass == true) {
            System.out.println("考试通过,恭喜");
        } else {
            System.out.println("考试没有通过,下次努力");
        }
    }
}

基本数据类型转换

自动类型转换

自动类型转换注意和细节

public class AutoConvertDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
        //细节 1: 有多种类型的数据混合运算时,
        //系统首先自动将所有数据转换成容量最大的那种数据类型,然后再进行计算
        int n1 = 10; //ok
        //float d1 = n1 + 1.1;//错误 n1 + 1.1 => 结果类型是 double
        //double d1 = n1 + 1.1;//对 n1 + 1.1 => 结果类型是 double
        float d1 = n1 + 1.1F;//对 n1 + 1.1 => 结果类型是 float
        //细节 2: 当我们把精度(容量)大 的数据类型赋值给精度(容量)小 的数据类型时,//就会报错,反之就会进行自动类型转换。
        //
        //int n2 = 1.1;//错误 double -> int
        //细节 3: (byte, short) 和 char 之间不会相互自动转换
        //当把具体数赋给 byte 时,(1)先判断该数是否在 byte 范围内,如果是就可以byte b1 = 10; //对 , -128-127
        // int n2 = 1; //n2 是 int
        // byte b2 = n2; //错误,原因: 如果是变量赋值,判断类型
        //
        // char c1 = b1; //错误, 原因 byte 不能自动转成 char
        //细节 4: byte,short,char 他们三者可以计算,在计算时首先转换为 int 类型
        byte b2 = 1;
        byte b3 = 2;
        short s1 = 1;
        //short s2 = b2 + s1;//错, b2 + s1 => int
        int s2 = b2 + s1;//对, b2 + s1 => int
        //byte b4 = b2 + b3; //错误: b2 + b3 => int
        //
        //boolean 不参与转换
        boolean pass = true;
        //int num100 = pass;// boolean 不参与类型的自动转换
        //自动提升原则: 表达式结果的类型自动提升为 操作数中最大的类型
        //看一道题
        byte b4 = 1;
        short s3 = 100;
        int num200 = 1;
        float num300 = 1.1F;
        double num500 = b4 + s3 + num200 + num300; //float -> double
    }
}

强制类型转换

✓ 介绍

自动类型转换的逆过程,将容量大的数据类型转换为容量小的数据类型。使用时要加上强制转换符( ),但可能造成精度降低或溢出,格外要注意。

✓ 案例演示 ForceConvert.java

public class ForceConvert {
    public static void main(String[] args) {
        //演示强制类型转化
        int i = (int) 1.9;
        System.out.println(i);//1.造成精度损失

        int j = 2000;
        byte b1 =(byte) j;
        System.out.println(b1);//造成 数据溢出
    }
}

强制类型转换细节说明

public class ForceConvertDetail {
    //编写一个 main 方法
    public static void main(String[] args) {
        //演示强制类型转换
        //强转符号只针对于最近的操作数有效,往往会使用小括号提升优先级
        //int x = (int)10*3.5+6*1.5;//编译错误: double -> int
        int x = (int) (10 * 3.5 + 6 * 1.5);// (int)44.0 -> 44
        System.out.println(x);//44
        char c1 = 100; //ok
        int m = 100; //ok
        //char c2 = m; //错误
        char c3 = (char) m; //ok
        System.out.println(c3);//100 对应的字符, d 字符
    }
}

基本数据类型转换-练习题

基本数据类型和 String 类型的转换

介绍和使用

public class StringToBasic {
    public static void main(String[] args) {
        //基本数据类型->String
        int n1 = 100;
        float f1 = 1.1F;
        double d1 = 4.5;
        boolean b1 = true;
        String s1 = n1 + "";
        String s2 = f1 + "";
        String s3 = d1 + "";
        String s4 = b1 + "";
        System.out.println(s1 + " " + s2 + " " + s3 + " " + s4);
        //String->对应的基本数据类型
        String s5 = "123";
        //会在 OOP 讲对象和方法的时候回详细
        //解读 使用 基本数据类型对应的包装类,的相应方法,得到基本数据类型
        int num1 = Integer.parseInt(s5);
        double num2 = Double.parseDouble(s5);
        float num3 = Float.parseFloat(s5);
        long num4 = Long.parseLong(s5);
        byte num5 = Byte.parseByte(s5);
        boolean b = Boolean.parseBoolean("true");
        short num6 = Short.parseShort(s5);
        System.out.println("===================");
        System.out.println(num1);//123
        System.out.println(num2);//123.0
        System.out.println(num3);//123.0
        System.out.println(num4);//123
        System.out.println(num5);//123
        System.out.println(num6);//123
        System.out.println(b);//true
        //怎么把字符串转成字符 char -> 含义是指 把字符串的第一个字符得到
        //解读 s5.charAt(0) 得到 s5 字符串的第一个字符 '1' System.out.println(s5.charAt(0));
    }
}

注意事项

案例演示: StringToBasicDetail.java

1.在将 String 类型转成 基本数据类型时, ,比如我们可以把"123" , 转成一个整数,但是不能把 “hello” 转成一个整数

  1. 如果格式不正确,就会抛出异常,程序就会终止。
public class StringToBasic {
    public static void main(String[] args) {
        String str = "hello";
        //转成 int
        int n1 = Integer.parseInt(str);
        System.out.println(n1);
    }
}

作业练习

public class HomeWork03 {
    public static void main(String[] args) {

        String book1 = "天龙八部";
        String book2 = "笑傲江湖";
        System.out.println(book1+book2);

        //性别应该用char保存
        char c1 = '男';
        char c2 = '女';
        System.out.println(c1+c2);

        //保存两本书的价格
        double price1 = 123.56;
        double price2 = 100.11;
        System.out.println(price1+price2);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值