Java - Tips - 01

How do you determine the size of primitive data types in JAVA?

  • sizeof() operator in C is used to determine the size of its operand (memory (RAM) required) 
  • In JAVA, there is no sizeof() operator as in C

Wrapper classes: 

  • It provides a way to use primitive data typesas objects 
  • Byte, Short, Integer, Long, Float, Double, Character and Boolean are the wrapper classes for the primitive data types byte, short, int, long, float, double, char and boolean respectively
  • Except for the Boolean wrapper class, all other classes have SIZE and BYTES attributes
  • The SIZE attribute determines the size of the data type in bits whereas, the BYTES returns the size of the datatype in bytes.

Example:


class DetermineSize 
     public static void main(String args[])
     {
             System.out.println("size of byte (in bits)="+Byte.SIZE+" bits"); 
             System.out.println("size of byte (in bytes)="+Byte.BYTES+" byte"); 
             System.out.println("size of short (in bits)="+Short.SIZE+" bits"); 
             System.out.println("size of short (in bytes)="+Short.BYTES+" bytes"); 
             System.out.println("size of int (in bits)="+Integer.SIZE+" bits");
             System.out.println("size of int (in bytes)="+Integer.BYTES+" bytes");
             System.out.println("size of long (in bits)="+Long.SIZE+" bits");
             System.out.println("size of long (in bytes)="+Long.BYTES+" bytes");
             System.out.println("size of float (in bits)="+Float.SIZE+" bits");
             System.out.println("size of float (in bytes)="+Float.BYTES+" bytes");
             System.out.println("size of double (in bits)="+Double.SIZE+" bits"); 
             System.out.println("size of double (in bytes)="+Double.BYTES+" bytes"); 
             System.out.println("size of char (in bits)="+Character.SIZE+" bits");
             System.out.println("size of char(in bytes)="+Character.BYTES+" bytes");
      }
}

OUTPUT:


size of byte (in bits)=8 bits
size of byte (in bytes)=1 byte
size of short (in bits)=16 bits
size of short (in bytes)=2 bytes
size of int (in bits)=32 bits
size of int (in bytes)=4 bytes
size of long (in bits)=64 bits
size of long (in bytes)=8 bytes
size of float (in bits)=32 bits
size of float (in bytes)=4 bytes
size of double (in bits)=64 bits
size of double (in bytes)=8 bytes
size of char (in bits)=16 bits
size of char(in bytes)=2 bytes

Comments

Popular posts from this blog

Python Tips -06

C Puzzle

Python Tips - 03