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 thus you lot mightiness possess got noticed roughly divergence betwixt Java as well as C programming language, e.g., String is an object inward Java as well as non a NULL-terminated grapheme array. Similarly, at that topographic point is is no sizeof() operator inward Java. All primitive values possess got predefined size, e.g., int is four bytes, char is ii byte, curt is ii byte, long as well as float is 8 byte as well as thus on. But if you lot are missing sizeOf operator thus why non let's acquire into a coding task? If you lot are Ok, thus your side yesteryear side labor is to write a method inward Java, which tin behaviour similar sizeOf() operator/function of C as well as returns the size inward bytes for each primitive numeric 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 1 chip to stand upwards 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 Federal Reserve notation if this is interesting as well as challenging.




Java sizeof() portion Example

Here is our consummate Java computer program to implement the sizeof operator. It's non just the size, but its purpose is the same. sizeof returns how much retention a detail information type takes, as well as this method does just that.


/**  * Java Program to impress size of primitive information types e.g. byte, int, short, double, float  * char, curt 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 curt 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 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 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 mean value of taking payoff of the pre-defined size of Java information types, neither you lot mean value 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 thus allow us know.

Further Learning
The Complete Java MasterClass
solution)
  • 10 Data Structure as well as Algorithms courses for Interviews (courses)
  • Reverse String inward Java using Iteration as well as Recursion? (solution)
  • Count the divulge of vowels as well as consonants inward a String? (solution)
  • Find duplicate characters inward a String? (solution)
  • Count the occurrence of a given grapheme inward String? (solution)
  • My favorite gratuitous class to larn Data Structure as well as Algorithms (courses)
  • Convert numeric string to an int? (solution)
  • 50+ Data Structure as well as Algorithms Interview Questions (list)
  • Check if String is Palindrome? (solution)
  • Find the highest occurred grapheme inward a String? (solution)
  • Check if a String contains alone digits?  (solution)
  • Find the outset non-repeated grapheme from String? (solution)
  • Reverse words inward a judgement without using a library method? (solution)
  • Reverse a String inward house inward Java? (solution)

  • Thanks for reading this article thus far. If you lot similar this coding or algorithm interview query as well as my explanation thus delight percentage alongside your friends as well as colleagues. If you lot possess got whatsoever uncertainty or feedback thus delight drib a note.

    P.S. - If you lot are preparing for Programming Job Interview as well as you lot demand to a greater extent than such questions, tin banking enterprise gibe the Data Structures as well as Algorithms Bootcamp yesteryear Jonathan Rasmusson class on  Udemy.

    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