zoukankan      html  css  js  c++  java
  • java类型占用字节数&类型转换

    1.整型
    类型              存储需求     bit数    取值范围      备注
    int                 4字节           4*8 
    short             2字节           2*8    -32768~32767
    long              8字节           8*8
    byte              1字节           1*8     -128~127

    2.浮点型
    类型              存储需求     bit数    取值范围      备注
    float              4字节           4*8                    float类型的数值有一个后缀F(例如:3.14F)
    double          8字节           8*8                    没有后缀F的浮点数值(如3.14)默认为double类型

    3.char类型
    类型              存储需求     bit数     取值范围      备注
    char              2字节          2*8

    4.boolean类型
    类型              存储需求    bit数    取值范围      备注
    boolean        1字节          1*8      false、true

    /**
     * 类型转换工具类
     * Created by xingxing.dxx on 2016/6/2.
     */
    public class TypeConversionUtil {
    
        /**
         * short占两个字节
         * @param n
         * @return
         */
        public static byte[] shortToBytes(short n) {
            byte[] b = new byte[2];
            for (int i = 0; i < 2; i++) {
                b[i] = (byte) (n >> i * 8);
            }
            return b;
        }
    
        /**
         * int 占4个字节
         * @param n
         * @return
         */
        public static byte[] intToBytes(int n) {
            byte[] b = new byte[4];
            for (int i = 0; i < 4; i++) {
                b[i] = (byte) (n >> (i * 8));
            }
            return b;
        }
    
        /**
         * byte数组转int类型
         *
         * @param src
         * @return
         */
        public static int bytesToInt(byte[] src) {
            int offset = 0;
            int value;
            value = (int) ((src[offset] & 0xFF)
                    | ((src[offset + 1] & 0xFF) << 8)
                    | ((src[offset + 2] & 0xFF) << 16)
                    | ((src[offset + 3] & 0xFF) << 24));
            return value;
        }
    }

     最后附上常用的类型转换

    1GB=1024MB 1MB=1024KB 1KB=1024字节 1字节=8位

  • 相关阅读:
    关于Java 下 Snappy压缩存文件
    英文分词和中文分词
    vuex requires a Promise polyfill in this browser
    Vue 中 export default 和 module.exports
    Linux 进程以及多线程的支持
    mysqldump
    linux磁 盘分区 挂载
    ubuntu16.04挂载windows NTFS磁盘方法
    Linux服务管理 systemctl命令详解
    nextcloud 安装
  • 原文地址:https://www.cnblogs.com/duanxingxing/p/5581326.html
Copyright © 2011-2022 走看看