自己可以做拼单网站吗,wordpress图文简介文章页,用div做网站中间部分,深圳做微信商城网站java类似sizeof如果您刚开始学习Java并且是C语言背景#xff0c;那么您可能已经注意到Java和C编程语言之间存在一些差异#xff0c;例如String是Java中的对象#xff0c;而不是NULL终止的字符数组。 同样#xff0c;Java中没有sizeof#xff08;#xff09;运算符。 所有… java类似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 languages 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.htmljava类似sizeof