Q:

Java Program to Find Size of Different Datatype

0

Java Program to Find Size of Different Data type or Find Size of Primitive Data Type in Java or Data types in java with example programs or java Program to Find the Size of int, float, double and char or Java Variables and (Primitive) Data Types or how to find size of variable in java or Find Size of Primitive Data Type in Java or How to find the integer length in java? or size of the variable in java.Write A Java Program To Display Size Of Different Datatype.

 

Logic:-
 As we know that C++ has a "sizeof" operator to find a size of any data type or variable. But in Java, we can find the size of a variable by this (Integer.SIZE/8), I want to clear one thing the size of data type maybe depend upon your operating system ( you Are using maybe 32 Bit or 64 Bit ). Below is a syntax of sizeof operator in C++.

Syntax:- sizeof( datatype )

Just replace datatype in above syntax by the following datatype and you are done, size operator displays the size of each variable on the screen.

int
float
double
long double
long long

For Java, the syntax is below you have to only change the datatype but real data type name.

(datatype.size/8)
(Integer.SIZE/8)
(Long.SIZE/8)
(Character.SIZE/8)
(Float.SIZE/8)
(Double.SIZE/8)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.*;
class SizeofdataTypes
{
 public static void main (String[] args)
 {
  System.out.println("Size of int: " + (Integer.SIZE/8) + " bytes.");
  System.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: " + (Float.SIZE/8) + " bytes.");
  System.out.println("Size of double: " + (Double.SIZE/8) + " bytes.");
  }
}

 

Output:

Size of int: 4 bytes.

Size of long: 8 bytes.

Size of char: 2 bytes.

Size of float: 4 bytes.

Size of double: 8 bytes.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now