Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the difference between Long.valueOf(0) and 0L in Java?

Tags:

java

types

I would like to know the difference between using Long.valueOf(0); or 0L. Are they the same?

I understand that both are Long types so they have a memory consumption of 64-bits long in Java 8.

So how it is better to initialize a variable, considering memory consumption and time complexity?

Long a = Long.valueOf(0);

or

Long b = 0L;

like image 275
Alex Blasco Avatar asked May 28 '19 13:05

Alex Blasco


People also ask

What is value of 0L in Java?

The 0L means the number zero of type long . It uses this constructor to instantiate a Date that refers to zero milliseconds after (i.e. exactly) "the epoch", January 1, 1970, 00:00:00 GMT.

Is 0 a long value in Java?

The long contains minimum value of -263 and a maximum value of 263-1. Its default value is 0L. Its default size is 8 byte.

How do you write a long zero in Java?

The == operator in Java checks for object equality. With new Long(0) you explicitly create a new object that will never have the same identity as any other Long. Use the equals method, but beware of NullPointerExceptions`.

Is 1 is a long value?

However in the first case the number (1) is evaluated as integer whereas 1L is always a long.


2 Answers

Nothing.

Long b = 0L;

will undergo autoboxing. The compiler replaces it with:

Long b = Long.valueOf(0L);

You can see this if you decompile your class, e.g. using javap.

void a() {
  Long a = Long.valueOf(0);
}

void b() {
  Long b = 0L;
}

Decompiles to:

  void a();
    Code:
       0: lconst_0
       1: invokestatic  #2                  // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
       4: astore_1
       5: return

  void b();
    Code:
       0: lconst_0
       1: invokestatic  #2                  // Method java/lang/Long.valueOf:(J)Ljava/lang/Long;
       4: astore_1
       5: return

So how it is better initialize a variable, considering memory consumption and time complexity?

Because they are semantically identical, the memory consumption and time complexity is also identical.

Instead, focus on what is actually important, which is readability: use the one you (and others) will find most easy to understand at a glance.

like image 170
Andy Turner Avatar answered Oct 22 '22 00:10

Andy Turner


As others have pointed out, Long l = Long.valueOf(0) and Long l = 0L will compile to the same bytecode, the only difference is style and readability.

Additionally..

It's a bit silly to worry about time complexity for something like this: both expressions are constant time. You usually only talk about time complexity when acting on collections of data not just a single piece of data.

As for memory consumption, they do not use 64 bits as you say; It's the primitive long type that typically uses 64 bits but Long (the wrapper type) uses more memory than the primitive type because it needs that memory for object-related stuff.

like image 29
xtratic - Reinstate Monica Avatar answered Oct 22 '22 00:10

xtratic - Reinstate Monica