如何用Java编写类似C的Sizeof函数

如果您刚开始学习Java并且是C语言背景,那么您可能已经注意到Java和C编程语言之间存在一些差异,例如String是Java中的对象,而不是NULL终止的字符数组。 同样,Java中没有sizeof()运算符。 所有原始值都有预定义的大小,例如int是4个字节,char是2个字节,short是2个字节,long和float是8个字节,依此类推。 但是,如果您缺少sizeOf运算符,那为什么不让它成为编码任务呢? 如果确定,那么您的下一个任务是用Java写一个方法,该方法的行为类似于C的sizeOf()运算符/函数,并为每种数字基本类型(即除Boolean之外的所有基本类型)返回以字节为单位的大小。

你们中许多人认为,为什么我们不包括布尔值? 难道它只需要1位代表真假值? 好吧,我不在本练习中包括布尔值,因为布尔值大小在Java规范中没有严格定义,并且在不同的JVM之间有所不同。

另外,据您所知, Java中原语的大小是固定的,它与平台无关。

因此,在32位和64位计算机上,int基本变量在Windows和Linux中都将占用4个字节。

无论如何,这是Java中不同基本类型的大小和默认值供您参考:

大小

现在由您的创造力来给出多个答案,但是我们至少需要一个答案来解决此编码问题。 如果你们喜欢这个问题,那么我可能会将其列入75个可以破解任何编程工作面试的编码问题列表中,如果这有趣且具有挑战性,请写下笔记。

Java sizeof()函数示例

这是我们实现sizeof运算符的完整Java程序。 它的大小不完全相同,但目的是相同的。 sizeof返回特定数据类型占用的内存,而此方法正是这样做的。

/**
 * Java Program to print size of primitive data types e.g. byte, int, short, double, float
 * char, short etc, in a method like C programming language's sizeof
 *
 * @author Javin Paul
 */
public class SizeOf{

    public static void main(String args[]) {

        System.out.println(" size of byte in Java is (in bytes) :  "
    + sizeof(byte.class));
        System.out.println(" size of short in Java is (in bytes) :" 
    + sizeof(short.class));
        System.out.println(" size of char in Java is (in bytes) :" 
    + sizeof(char.class));
        System.out.println(" size of int in Java is (in bytes) :" 
    + sizeof(int.class));
        System.out.println(" size of long in Java is (in bytes) :" 
    + sizeof(long.class));
        System.out.println(" size of float in Java is (in bytes) :" 
    + sizeof(float.class));
        System.out.println(" size of double in Java is (in bytes) :" 
    + sizeof(double.class));

    }


    /*
     * Java method to return size of primitive data type based on hard coded values
     * valid but provided by developer
     */
    public static int sizeof(Class dataType) {
        if (dataType == null) {
            throw new NullPointerException();
        }
        if (dataType == byte.class || dataType == Byte.class) {
            return 1;
        }
        if (dataType == short.class || dataType == Short.class) {
            return 2;
        }
        if (dataType == char.class || dataType == Character.class) {
            return 2;
        }
        if (dataType == int.class || dataType == Integer.class) {
            return 4;
        }
        if (dataType == long.class || dataType == Long.class) {
            return 8;
        }
        if (dataType == float.class || dataType == Float.class) {
            return 4;
        }
        if (dataType == double.class || dataType == Double.class) {
            return 8;
        }
        return 4; // default for 32-bit memory pointer
    }


    /*
     * A perfect way of creating confusing method name, sizeof and sizeOf
     * this method take advantage of SIZE constant from wrapper class
     */
    public static int sizeOf(Class dataType) {
        if (dataType == null) {
            throw new NullPointerException();
        }
        if (dataType == byte.class || dataType == Byte.class) {
            return Byte.SIZE;
        }
        if (dataType == short.class || dataType == Short.class) {
            return Short.SIZE;
        }
        if (dataType == char.class || dataType == Character.class) {
            return Character.SIZE;
        }
        if (dataType == int.class || dataType == Integer.class) {
            return Integer.SIZE;
        }
        if (dataType == long.class || dataType == Long.class) {
            return Long.SIZE;
        }
        if (dataType == float.class || dataType == Float.class) {
            return Float.SIZE;
        }
        if (dataType == double.class || dataType == Double.class) {
            return Double.SIZE;
        }
        return 4; // default for 32-bit memory pointer
    }
}

Output:
size of byte in Java is (in bytes) :  1
size of short in Java is (in bytes) :2
size of char in Java is (in bytes) :2
size of int in Java is (in bytes) :4
size of long in Java is (in bytes) :8
size of float in Java is (in bytes) :4
size of double in Java is (in bytes) :8

这就是编写sizeof就像Java中的方法一样的编程工作。 这实际上很棘手,因为您不会考虑利用Java数据类型的预定义大小,也不会考虑利用
包装类中定义的SIZE常数,例如Integer或Double。 好吧,如果您可以通过其他任何方式找到原始数据类型的大小,请告诉我们。

翻译自: https://www.javacodegeeks.com/2018/06/sizeof-function-java.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值