Primitive type Default Byte Size - Java java.lang

Java examples for java.lang:Number

Description

Primitive type Default Byte Size

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class type = String.class;
        System.out.println(type2DefaultByteSize(type));
    }/*from   w w  w . ja  va2  s. c o  m*/

    public static int type2DefaultByteSize(Class<?> type) {
        int ret = -1;
        if (type.equals(byte.class) || type.equals(Byte.class)) {
            ret = 1;
        } else if (type.equals(short.class) || type.equals(Short.class)) {
            ret = 2;
        } else if (type.equals(int.class) || type.equals(Integer.class)) {
            ret = 4;
        } else if (type.equals(long.class) || type.equals(Long.class)) {
            ret = 8;
        } else if (type.equals(float.class) || type.equals(Float.class)) {
            ret = 4;
        } else if (type.equals(double.class) || type.equals(Double.class)) {
            ret = 8;
        } else {
            ret = -1;
        }

        return ret;
    }
}

Related Tutorials