How To Write A C Similar Sizeof Business Office Inwards Java
If you lot conduct keep only started learning Java as well as came from C background as well as then you lot mightiness conduct keep noticed about departure betwixt Java as well as C programming linguistic communication e.g. String is an object inward Java as well as non a NULL terminated graphic symbol array. Similarly, at that topographic point is is no sizeof() operator inward Java. All primitive values conduct keep predefined size e.g. int is iv bytes, char is ii byte, brusk is ii byte, long as well as float is 8 byte as well as thence on. But if you lot are missing sizeOf operator as well as then why non let's movement into a coding task? If you lot are Ok, as well as then your adjacent business is to write a method inward Java, which tin move behavior similar sizeOf() operator/function of C as well as returns size inward bytes for each numeric primitive types i.e. all primitive types except boolean.
Many of you lot think, why nosotros are non including boolean? isn't that it only demand i flake to stand upwardly for truthful as well as faux value? Well, I am nog including boolean inward this practise because the size of boolean is non strictly defined inward Java specification as well as varies betwixt dissimilar JVM (See 75 Coding Problems to Crack Any Programming Job interview, drib a greenback if this is interesting as well as challenging.
That's all inward this programming practise of writing a sizeof similar a method inward Java. This is genuinely tricky because, you lot don't intend of taking wages of the pre-defined size of Java information types, neither you lot intend well-nigh taking wages of SIZE constants defined inward wrapper classes e.g. Integer or Double. Well, if you lot tin move come upwardly across whatever other agency of finding the size of primitive information type as well as then allow us know.
Further Learning
The Complete Java MasterClass
Java Fundamentals: The Java Language
Data Structures as well as Algorithms: Deep Dive Using Java
Thanks for reading this article thence far. If you lot similar this article as well as then delight part amongst your friends as well as colleagues. If you lot conduct keep whatever questions or feedback as well as then delight drib a note.
Many of you lot think, why nosotros are non including boolean? isn't that it only demand i flake to stand upwardly for truthful as well as faux value? Well, I am nog including boolean inward this practise because the size of boolean is non strictly defined inward Java specification as well as varies betwixt dissimilar JVM (See 75 Coding Problems to Crack Any Programming Job interview, drib a greenback if this is interesting as well as challenging.
Java sizeof() component Example
Here is our consummate Java programme to implement sizeof operator. It's non precisely size but its purpose is same. sizeof returns how much retention a item information type accept as well as this method does precisely that./** * Java Program to impress size of primitive information types e.g. byte, int, short, double, float * char, brusk etc, inward a method similar C programming language's sizeof * * @author Javin Paul */ public class SizeOf{ public static void main(String args[]) { System.out.println(" size of byte inward Java is (in bytes) : " + sizeof(byte.class)); System.out.println(" size of brusk inward Java is (in bytes) :" + sizeof(short.class)); System.out.println(" size of char inward Java is (in bytes) :" + sizeof(char.class)); System.out.println(" size of int inward Java is (in bytes) :" + sizeof(int.class)); System.out.println(" size of long inward Java is (in bytes) :" + sizeof(long.class)); System.out.println(" size of float inward Java is (in bytes) :" + sizeof(float.class)); System.out.println(" size of double inward Java is (in bytes) :" + sizeof(double.class)); } /* * Java method to render size of primitive information type based on difficult coded values * valid but provided yesteryear 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 retention pointer } /* * H5N1 perfect agency of creating confusing method name, sizeof as well as sizeOf * this method accept wages 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 retention pointer } } Output: size of byte inward Java is (in bytes) : 1 size of short inward Java is (in bytes) :2 size of char inward Java is (in bytes) :2 size of int inward Java is (in bytes) :4 size of long inward Java is (in bytes) :8 size of float inward Java is (in bytes) :4 size of double inward Java is (in bytes) :8
That's all inward this programming practise of writing a sizeof similar a method inward Java. This is genuinely tricky because, you lot don't intend of taking wages of the pre-defined size of Java information types, neither you lot intend well-nigh taking wages of SIZE constants defined inward wrapper classes e.g. Integer or Double. Well, if you lot tin move come upwardly across whatever other agency of finding the size of primitive information type as well as then allow us know.
Further Learning
The Complete Java MasterClass
Java Fundamentals: The Java Language
Data Structures as well as Algorithms: Deep Dive Using Java
Thanks for reading this article thence far. If you lot similar this article as well as then delight part amongst your friends as well as colleagues. If you lot conduct keep whatever questions or feedback as well as then delight drib a note.
Belum ada Komentar untuk "How To Write A C Similar Sizeof Business Office Inwards Java"
Posting Komentar