Java Type Size sizeOf(Class clz)

Here you can find the source of sizeOf(Class clz)

Description

Get the size of a primitive type.

License

Apache License

Parameter

Parameter Description
clz a parameter

Exception

Parameter Description
IllegalArgumentException If the class is not primitive type.

Declaration

public static int sizeOf(Class<?> clz) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from  w  w  w .  j a  va2s  . com
     * Get the size of a primitive type.
     *
     * @param clz
     * @throws IllegalArgumentException If the class is not primitive type.
     * @return
     */
    public static int sizeOf(Class<?> clz) throws IllegalArgumentException {
        switch (clz.getName()) {
        case "int":
            return Integer.BYTES;
        case "short":
            return Short.BYTES;
        case "long":
            return Long.BYTES;
        case "double":
            return Double.BYTES;
        case "float":
            return Float.BYTES;
        case "boolean":
            return 1;
        case "char":
            return Character.BYTES;
        case "byte":
            return 1;
        default:
            throw new IllegalArgumentException("Not a primitive type.");
        }
    }
}

Related

  1. sizeOf(Class cls)
  2. sizeOf(double v)
  3. sizeOf(int fieldNumber)
  4. sizeOf(int tagNumber, int contentLength)
  5. sizeOf(int value)