[Java] Float and double

Asked 1 years ago, Updated 1 years ago, 74 views

I basically used double to store real numbers in Java.

However, there are float and double variable types that store real numbers.

You can enter double as soon as you initialize it.

double a = 1.5;

Like this.

By the way, in the case of float,

float b = 1.5f;

You have to type it like this.

If so, the double is actually better to use

Why do you use float?

Is it used to emphasize that because the size allocated is small in the first place?

java float double

2022-09-22 18:08

1 Answers

Double is a large 8-byte data type. Using double to store 1.5 would be a waste of memory.

Similarly, an integer data type has int long respectively.

From a jvm perspective, 8-byte data types occupy two 4-byte stacks. This means that more computation is needed, which can be a performance issue, not just memory usage.

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


2022-09-22 18:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.