How To Write A C Similar Sizeof Business Office Inwards Java

If you lot possess got only started learning Java as well as came from C background as well as hence you lot mightiness possess got noticed around 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 grapheme array. Similarly, at that spot is is no sizeof() operator inward Java. All primitive values possess got 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 hence on. But if you lot are missing sizeOf operator as well as hence why non let's survive into a coding task? If you lot are Ok, as well as hence your side past times side business is to write a method inward Java, which tin bear 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 chip to stand upwards for truthful as well as faux value? Well, I am nog including boolean inward this practice 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 banker's complaint if this is interesting as well as challenging.


Java sizeof() business office Example

Here is our consummate Java plan to implement sizeof operator. It's non precisely size but its purpose is same. sizeof returns how much retentiveness 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 past times 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 retentiveness pointer     }       /*      * Influenza A virus subtype H5N1 perfect agency of creating confusing method name, sizeof as well as sizeOf      * this method accept payoff 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 retentiveness 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 practice of writing a sizeof similar a method inward Java. This is genuinely tricky because, you lot don't intend of taking payoff of the pre-defined size of Java information types, neither you lot intend almost taking payoff of SIZE constants defined inward wrapper classes e.g. Integer or Double. Well, if you lot tin come upwards across whatsoever other agency of finding the size of primitive information type as well as hence 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 hence far. If you lot similar this article as well as hence delight portion amongst your friends as well as colleagues. If you lot possess got whatsoever questions or feedback as well as hence delight drib a note.



Sumber https://javarevisited.blogspot.com/

0 Response to "How To Write A C Similar Sizeof Business Office Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel