Tuesday, April 30, 2024
HomeJavaThe best way to write a C like sizeof() operate in Java?...

The best way to write a C like sizeof() operate in Java? [Solved]


When you’ve got simply began studying Java and got here from C background then you definately might need seen some distinction between Java and C programming language, e.g., String is an object in Java and never a NULL-terminated character array. Equally, there may be is not any sizeof() operator in Java. All primitive values have predefined measurement, e.g., int is 4 bytes, char is 2 byte, brief is 2 byte, lengthy and float is 8 byte, and so forth. However in case you are lacking sizeOf operator then why not let’s make it a coding job? In case you are Okay, then your subsequent job is to write down a way in Java, which may behave like sizeOf() operator/operate of C and returns the scale in bytes for every primitive numeric kind, i.e. all primitive sorts besides boolean.

Lots of you suppose, why we aren’t together with boolean? Is not that it simply want 1 bit to symbolize true and false worth? Properly, I’m nog, together with boolean on this train as a result of the measurement of boolean just isn’t strictly outlined in Java specification and varies between totally different JVM (See
Java Fundamentals: The Java Language).

Additionally, to your information, the measurement of primitives in Java is mounted, it would not rely on the platform.
So an int primitive variable will take 4 bytes in each Home windows and Linux, each on 32-bit and 64-bit machines.

Additionally, primary information of important information construction and algorithms can also be crucial and that is why I recommend all Java programmers be part of a complete Knowledge Construction and Algorithms course like Knowledge Constructions and Algorithms: Deep Dive Utilizing Java on Udemy to enhance your information and algorithms abilities.

Anyway, right here is the scale and default values of various primitive sorts in Java to your reference:

How to write a C like Sizeof function in Java

Now it is as much as your creativity to offer a number of solutions, however we’d like no less than one reply to resolve this coding downside.  If you happen to guys like this downside then I would embody this on my checklist of 75 Coding Issues to Crack Any Programming Job interview, drop a be aware if that is attention-grabbing and difficult.

Java sizeof() operate Instance

Right here is our full Java program to implement the sizeof operator. It is not precisely the scale, however its objective is similar. sizeof returns how a lot reminiscence a selected information kind takes, and this technique does precisely that.

/**
 * Java Program to print measurement of primitive information sorts e.g. byte, int, brief, double, float
 * char, brief and so on, in a way like C programming language's sizeof
 *
 * @writer Javin Paul
 */
public class SizeOf{

    public static void important(String args[]) {

        System.out.println(" measurement of byte in Java is (in bytes) :  "
    + sizeof(byte.class));
        System.out.println(" measurement of brief in Java is (in bytes) :" 
    + sizeof(brief.class));
        System.out.println(" measurement of char in Java is (in bytes) :" 
    + sizeof(char.class));
        System.out.println(" measurement of int in Java is (in bytes) :" 
    + sizeof(int.class));
        System.out.println(" measurement of lengthy in Java is (in bytes) :" 
    + sizeof(lengthy.class));
        System.out.println(" measurement of float in Java is (in bytes) :" 
    + sizeof(float.class));
        System.out.println(" measurement of double in Java is (in bytes) :" 
    + sizeof(double.class));

    }


    /*
     * Java technique to return measurement of primitive information kind primarily based on onerous coded values
     * legitimate however supplied 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 == brief.class || dataType == Quick.class) {
            return 2;
        }
        if (dataType == char.class || dataType == Character.class) {
            return 2;
        }
        if (dataType == int.class || dataType == Integer.class) {
            return 4;
        }
        if (dataType == lengthy.class || dataType == Lengthy.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 reminiscence pointer
    }


    /*
     * An ideal means of making complicated technique identify, sizeof and sizeOf
     * this technique benefit from SIZE fixed 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 == brief.class || dataType == Quick.class) {
            return Quick.SIZE;
        }
        if (dataType == char.class || dataType == Character.class) {
            return Character.SIZE;
        }
        if (dataType == int.class || dataType == Integer.class) {
            return Integer.SIZE;
        }
        if (dataType == lengthy.class || dataType == Lengthy.class) {
            return Lengthy.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 reminiscence pointer
    }
}

Output:
measurement of byte in Java is (in bytes) :  1
measurement of brief in Java is (in bytes) :2
measurement of char in Java is (in bytes) :2
measurement of int in Java is (in bytes) :4
measurement of lengthy in Java is (in bytes) :8
measurement of float in Java is (in bytes) :4
measurement of double in Java is (in bytes) :8

That is all on this programming train of writing a sizeof like a way in Java. That is truly difficult as a result of, you do not consider making the most of the pre-defined measurement of Java information sorts, neither you concentrate on making the most of SIZE constants outlined in wrapper courses, e.g. Integer or Double. Properly, in case you can come throughout every other means of discovering the scale of primitive information kind then tell us.

Additional Studying
The Full Java MasterClass
Java Fundamentals: The Java Language
Coursera’s Knowledge Construction & Algorithms Specialization
Knowledge Constructions and Algorithms: Deep Dive Utilizing Java

Different Coding Interview Questions you might like

  • Print duplicate characters from String? (answer)
  • 10 Knowledge Construction and Algorithms programs for Interviews (programs)
  • Reverse String in Java utilizing Iteration and Recursion? (answer)
  • 20+ Array-based Coding Issues for interviews (questions)
  • Depend the variety of vowels and consonants in a String? (answer)
  • 7 Greatest Programs to be taught Knowledge Construction and Algorithms (programs)
  • Discover duplicate characters in a String? (answer)
  • 10 Programs to be taught Java Programming for Rookies (programs)
  • Depend the prevalence of a given character in String? (answer)
  • My favourite free course to be taught Knowledge Construction and Algorithms (programs)
  • Convert numeric string to an int? (answer)
  • 50+ Knowledge Construction and Algorithms Interview Questions (checklist)
  • Examine if String is Palindrome? (answer)
  • 10 Books to be taught Knowledge Construction and Algorithms (books)
  • Discover the very best occurred character in a String? (answer)
  • Examine if a String accommodates solely digits?  (answer)
  • 10 Knowledge Construction, Algorithms, and Programming programs for interviews (programs)
  • Discover the primary non-repeated character from String? (answer)
  • Reverse phrases in a sentence with out utilizing a library technique? (answer)
  • Reverse a String in place in Java? (answer)

Thanks for studying this text to date. If you happen to like this coding or algorithm interview query and my clarification then please share it with your folks and colleagues. When you’ve got any doubts or suggestions then please drop a be aware.

P.S. – In case you are getting ready for Programming Job Interview and also you want extra such questions, can test the Knowledge Constructions and Algorithms Bootcamp by Jonathan Rasmusson course on  Udemy.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments