21/02/2023

Java Program to Find Size of Different Data Types

Java Program to Find the Size of Different Data types or Find the Size of Primitive Data Types 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 the size of variable in java or Find Size of Primitive Data Type in Java or size of the variable in java. Write a Java Program To Display the Size Of Different Datatype. Logic: As we know that C++ has a "sizeof" operator to find the 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 clarify one thing the size of the data type may depend upon your operating system (If you Are using maybe 32 Bit or 64 Bit). Below is a syntax of the sizeof operator in C++. Examsnap is a community of IT certification exam takers committed to sharing their exam knowledge and experience with their peers. Pass Your Next IT Certification Exam with A+ Certification.

Syntax:- sizeof( datatype )

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

int
float
char
double
long double
long long


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

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


See Also: C++ Program To Display Size Of Different Datatype

Java Program to Find Size of Different Data Types


import java.util.*;

/* how to find size of variable in java */

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.");
    }
}

See Also: C Program To Display Size Of Different Datatype

Size of Different Data Types


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

3 comments: