[Java] A function that displays the size of a variable, such as sizeof

Asked 1 years ago, Updated 1 years ago, 117 views

In C language, there is a function called sizeof as a function to check the size of a variable.

Is there any function or class in Java that acts like size of C?

If not, is there any way to print out the size of the variable?

java sizeof variable

2022-09-22 20:42

1 Answers

I don't have one.

The size of the variable is all objects except for the primary type. So it's an object, so it's a pointer to the variable c that points to the object. This size is the same, meaningless.

Below is the size of the primary type.

System.out.println("byte: " + Byte.BYTES);
System.out.println("char: " + Character.BYTES);
System.out.println("int: " + Integer.BYTES);
System.out.println("long: " + Long.BYTES);
System.out.println("short: " + Short.BYTES);
System.out.println("double: " + Double.BYTES);
System.out.println("float: " + Float.BYTES);

byte: 1
char: 2
int: 4
long: 8
short: 2
double: 8
float: 4

In Java, the array has .length and each collection (List, Map, etc.) is given a size method.


2022-09-22 20:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.