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位

  • 相关阅读:
    剑指offer-23.链表中环的入口节点
    剑指offer-6从尾到头打印链表
    剑指offer-24.反转链表
    2-常见机器学习模型总结
    1-预测分析类核心算法简介
    罗马数字转整数Leetcode13
    链表反转leetcode206
    LINUX常用命令
    两种遍历list
    python笔记-字符串函数总结
  • 原文地址:https://www.cnblogs.com/duanxingxing/p/5581326.html
Copyright © 2011-2022 走看看