How To Find Size Of Int In Java
How to find the size of a primitive data type in Java?
Java has no sizeof operator to find the size of primitive data types merely all Java primitive wrappers except Boolean
provide a SIZE
constant in bits that could be divided by 8 to get the size of a data type in bytes. Moreover, since Java 8, all primitive wrapper classes (except Boolean
) accept a BYTES
constant, which gives data type's size in bytes. Then you lot can use that besides one time you have been moved to Java viii. Following is a trivial Coffee program demonstrating the size of primitive information types through their primitive wrappers.
grade SizePrimitiveTypes { public static void principal ( String [ ] args) { Organisation.out.println ( "Size of byte: " + ( Byte.SIZE / 8 ) + " bytes." ) ; Organization.out.println ( "Size of short: " + ( Brusque.SIZE / viii ) + " bytes." ) ; System.out.println ( "Size of int: " + ( Integer.SIZE / 8 ) + " bytes." ) ; Organisation.out.println ( "Size of long: " + ( Long.SIZE / 8 ) + " bytes." ) ; System.out.println ( "Size of char: " + ( Character.SIZE / 8 ) + " bytes." ) ; System.out.println ( "Size of float: " + ( Bladder.SIZE / eight ) + " bytes." ) ; Arrangement.out.println ( "Size of double: " + ( Double.SIZE / 8 ) + " bytes." ) ; } } OUTPUT ====== D:\JavaPrograms>javac SizePrimitiveTypes.java D:\JavaPrograms>java SizePrimitiveTypes Size of byte : 1 bytes. Size of curt : 2 bytes. Size of int : 4 bytes. Size of long : 8 bytes. Size of char : two bytes. Size of bladder : 4 bytes. Size of double : 8 bytes.
Notation that size of primitive types in Java is always the same. It is not platform dependent. Also, all primitive information types in Java are signed. Coffee does not support unsigned types.
Hope you have enjoyed reading How to find the size of a archaic data type in Java? Please do write us if you accept any proffer/comment or come up across any error on this folio. Thanks for reading!
Most the Author
is the founder and main correspondent for cs-fundamentals.com. He is a software professional (mail graduated from $.25-Pilani) and loves writing technical articles on programming and data structures.
Source: https://cs-fundamentals.com/tech-interview/java/how-to-find-size-of-a-datatype-in-java
Posted by: thomashiplent.blogspot.com
0 Response to "How To Find Size Of Int In Java"
Post a Comment