5

I think it should be:

Long a = Long.valueOf(0);
Long b = Long.valueOf(1);
Long a = b.clone();

But there is no clone method in Long object...

Maybe Long a = Long.valueOf(b.LongValue()) will work, but it looks dirty. Is there any nice way to handle this?

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • 1
    `Long a = Long.valueOf(b.LongValue())` is correct – Pphoenix Jul 10 '14 at 06:44
  • 1
    Another one: Long myLong = new Long(otherLong); – morgano Jul 10 '14 at 06:45
  • 9
    Why do you need to do this? Long's immutable, so wanting a cloned instance is a smell for there being an issue elsewhere. – David Ehrmann Jul 10 '14 at 06:45
  • 1
    Long is immutable. There is no reason to create a copy of one. – Kevin Krumwiede Jul 10 '14 at 06:47
  • @DavidEhrmann I don't know why immutable and clone-able is conflict... could you give me any explanation? – Sayakiss Jul 10 '14 at 06:54
  • 1
    Immutable: http://stackoverflow.com/questions/279507/what-is-meant-by-immutable – jr. Jul 10 '14 at 06:54
  • Cloneable: http://stackoverflow.com/questions/16315628/why-shouldnt-an-object-be-cloneable – jr. Jul 10 '14 at 06:55
  • @Sayakiss It's not really a conflict as much as it doesn't buy you anything, and needing it is a sign you're doing something wrong. I used it once to quickly fix some code (`synchronized (myLong) { }` is a really bad idea). I could also see someone doing `IdentityHashMap`, but using cloned longs there is a horrible abuse. – David Ehrmann Jul 10 '14 at 16:17

3 Answers3

13

I think, because Long is immutable, you can just use an assignment.

Long a = (long) 1;
Long b = a;
a = (long) 2;
System.out.printf("a=%d, b=%d%n", a, b);

Output is

a=2, b=1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 2
    +1. Unless we get a convincing explanation for why a fresh instance is needed, simple assignment seems best. – Thilo Jul 10 '14 at 06:52
7

Something that looks much cleaner in my opinion:

Long a = b.LongValue()

There will be an implicit casting to the boxed value. The b.LongValue() will return the primitive long and it will automatically be boxed into an object of type Long.

You can read about boxing and auto-boxing in this link for more information.

Here's the section that talks about autoboxing of primitives:

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

Here is the simplest example of autoboxing:

Character ch = 'a';

Just for the completeness of the question:

  • Boxing - The process of taking a primitive and use it in it's wrapper class. Meaning boxing long in Long or int in Integer, etc... You can get a boxed value either by creating a new object of the wrapper class as follows:

    Long boxed = new Long(1);
    

    Or by assigning a primitive to a variable of type of a wrapper class (auto-boxing):

    Long boxed = 1l;
    
  • Unboxing - The opposite process of boxing. That's the process of taking a boxed parameter and get its primitive. Either by getValue() or by auto-unboxing as follows:

    Long boxed = new Long(1);
    long unboxed = boxed.getValue();
    

    Or by just assigning a boxed object to a primitive:

    long unboxed = new Long(1);
    
Avi
  • 21,182
  • 26
  • 82
  • 121
3

You can do it in many ways :

Long a = b.LongValue(); // Autoboxing

OR

Long a = new Long(b);
earthmover
  • 4,395
  • 10
  • 43
  • 74