如何用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
總結
以上是生活随笔為你收集整理的如何用Java编写类似C的Sizeof函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑时钟屏保风景(电脑桌面时钟屏保)
- 下一篇: Java正成为COBOL的一部分-它将成