Java学习(数据类型)

数据类型

Java的数据类型可以分为两种:基本数据类型以及引用数据类型。

基本数据类型

Java的基本数据类型有8种:byte、short、int、long、float、double、char、boolean。byte、short、int、long是整数类型;float、double是浮点数类型;char是字符型;boolean是布尔型。

它们的取值范围如下表所示:

类型描述位数字节取值范围默认值包装类
byte字节型81-128~1270Byte
short短整型162-2^15~2^15-10Short
int整型324-2^31~2^31-10Integer
long长整型648-2^63~2^63-10Long
float单精度浮点型3241.4e-45f~3.4e+38f0.0Float
double双精度浮点型6484.9e-324~1.8e+3080.0Double
char字符型162\u0000~\uffff或0~65535\u0000或0Character
boolean布尔型  只有true和falsefalseBoolean

浮点数类型采用的是IEEE754标准。

public class Demo {
    public static void main(String[] args) {
        byte a = 10;
        short b = 15;
        int c = 20;
        long d = 25;
        long i = 1000000000000l;//当long初始化的值比int大的时候需要加上l或L
        float e = 0.7f;//float初始化需要在后面加上f或F
        double f = 0.8;
        char g = 'a';
        boolean h = false;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        System.out.println("d = " + d);
        System.out.println("e = " + e);
        System.out.println("g = " + g);
        System.out.println("f = " + f);
        System.out.println("h = " + h);
        System.out.println("i = " + i);
    }
}

引用数据类型

引用类型是指向一个对象,不是一个原始值,在Java中除了基本数据类型之外其他的数据类型都是引用类型。用的比较多的引用类型是String,StringBuffer,数组等。

public class Demo {
    public static void main(String[] args) {
        String str = "Hello World";
        int [] array = new int[]{1,2,3,4,5};
        System.out.println("str = " + str);//String重写了toString方法,所以输出的是Hello World
        System.out.println("array = " + array);//输出地址,而不是值
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值