8

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;

Alex Blasco
  • 793
  • 11
  • 22
  • 3
    "I understand that both are Long types" - `Long#valueOf` returns an object of type `Long`, and `0L` is a primitive `long`. – Jacob G. May 28 '19 at 13:48
  • 1
    `Long::value` will give you an `Object`, `0L` a primitive (that you can box via `Long b = OL;` - that internally will still use `Long::valueOf`) – Eugene May 28 '19 at 13:48
  • 1
    Side note: in the range [-128,127] `Long.valueOf(x)` will return the same instance while every other primitive will cause a new instance to be created, i.e. you'll get a `new Long(x)` every time. – Thomas May 28 '19 at 13:51
  • 1
    @Thomas, That's interesting.... Where can I read more about it? – lealceldeiro May 28 '19 at 13:52
  • 2
    @lealceldeiro the JavaDoc and the source of `valueOf()` for `Long`, `Integer`, `Short`, `Byte` and `Character` tell you. :) Some more information can be found here: https://stackoverflow.com/questions/20897020/why-integer-class-caching-values-in-the-range-128-to-127 – Thomas May 28 '19 at 13:55
  • What is not covered in your question, the difference from `Long` <-> `long`. Could you edit your question to include this? Like : `Long a = Long.valueOf(0);Long b = OL; long c = 0L;` – Sunchezz May 28 '19 at 14:01
  • It is a mistake to use `Long` in cases where `long` will do. The memory usage of `Long` is several times that of `long`. – Boann May 28 '19 at 17:16

2 Answers2

16

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
5

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.

xtratic
  • 4,600
  • 2
  • 14
  • 32