Primitive Types in Java

本文简述Java中基本类型的一些知识点,属于个人学习总结,能力有限,还望各位大神多多指点。

Abstract

There are two types of data in Java:

  • primitive types: boolean, char, byte, short, int, long, float, and double.
  • reference types: class, interface, and array.

We only discuss primitive types here.

Java有两种数据类型:

  • 原生类型:boolean、char、byte、short、int、long、float和double。
  • 引用类型:class、interface和array。

在这里我们只讨论原生类型。

Primitive types are not Java Objects, but each of them has a wrapper class. When a wrapper class instance is required, but relative primity type is provided, the primitive type can be converted to its wrapper class instance. This is called auto-boxing. A wrapper class instance can be converted to relative primitive type automatically when necessary, this is called auto-unboxing.

原生类型不是Java对象,但是他们都有各自的封装类。在需要的时候,原生类型和其包装类之间可以自动转换。

public class Test {
   

    private static void testInt2Integer(Integer i) {
   
        System.out.println(i);
    }

    private static void testInteger2Int(int i) {
   
        System.out.println(i);
    }

    public static void main(String[] args) {
   
        int i = 10;
        Integer iObj = Integer.valueOf(20);

        // 10
        testInt2Integer(i);
        // 20
        testInteger2Int(iObj);
    }
}

integers

char, byte, short, int, long are all integers actually. All integer literals can be represented as hexadecimal, decimal, octal, or binary. In addition, underscore _ can be added between digits for ease of reading, especially for values that have many digits.

char、byte、short、int、long都是整型,所有整型的值都可以表示为16进制、10进制、8进制和2进制。另外两个数字间可以添加下划线以便于阅读,尤其是对很长的值。添加下划线对数值没有任何影响,仅仅是为了人类阅读方便而已。

public class Test {
   

    public static void main(String[] args) {
   

        // hexadecimal representation starts with 0x or 0X. I prefer to use lowercase x and uppercase digits A ~ F.
        byte b1 = 0x1F;
        byte b2 = 0X1f;

        // decimal representation doesn't have prefix and postfix.
        short s1 = -123;

        // octal representation starts with 0
        int i1 = 07777;

        // binary representation starts with 0b or 0B. I prefer to use lowercase b
        // long literal has postfix l or L. I prefer to use uppercase L.
        long l1 = 0b010101L;
        long l2 = 0B010101l;
        
        // It's better to add the postfix always. Because when postfix is omitted, actually it's casting an int to long.
        // So, if an int can't hold the long value, compile error happens.
        long l3 = 1234567890; // not recommended
        long l4 = 12345678900L; // OK
        long l5 = 12345678900; // compile error: The literal 12345678900 of type int is out of range

        // add underscores
        char c1 = 0x1_F;
        char c2 = 12___34;
        char c3 = 0_777;
        char c4 = 0b1_111_111_111;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值