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位

  • 相关阅读:
    交换机工作原理
    MyBatis框架的使用及源码分析(一) 配置与使用
    MySQL5.6安装步骤
    mysql创建用户,并授权
    命令行访问远程mysql数据库
    [mybatis] mybatis错误:Invalid bound statement (not found)
    【大数据】每秒十万笔交易的数据架构解读
    【mybaits】Mybatis中模糊查询的各种写法
    【redis】 linux 下redis 集群环境搭建
    [linux] linux下编译安装zlib
  • 原文地址:https://www.cnblogs.com/duanxingxing/p/5581326.html
Copyright © 2011-2022 走看看