Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between long.Class and Long.TYPE

Do they both return the same thing i.e Long Class. Actually i was using this within PrivilegedAccessor to pass as following

PrivilegedAccessor.invokeMethod(MyClass,
                "MyMethod", new Object[] { arg1, arg2 },
                new Class[] { long.class, Date.class });

Alternatively I can use

PrivilegedAccessor.invokeMethod(MyClass,
                    "MyMethod", new Object[] { arg1, arg2 },
                    new Class[] { Long.TYPE, Date.class });

Which is better to be used keeping in mind autoboxing / unboxing overheads.

** I am passing primitive long from the Test and even the tested method expects primitive long only.

like image 535
Vivek Vermani Avatar asked Jan 30 '14 18:01

Vivek Vermani


People also ask

What is the difference between long and long Java?

A Long is a class, or a reference type, defined in the standard library. It stores a reference to an object containing a value (a "box"). A long on the other hand, is a primitive type and part of the language itself.

What is difference between long and long in C#?

long is primitive type and Long is boxed type of long . After auto-boxing feature is released in java the primitive long can be automatically converted to Long , which is an object. This is because the program unintentionally creating 2^31 objects unnecessarily because of capital L in sum declaration.

What is type Long Java?

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.

What is long function in Java?

The LongFunction Interface is a part of the java. util. function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a long-valued argument and produces a result of type R.


1 Answers

They both represent the long primitive type. They are exactly the same, even in the compiled bytecode. Sample program:

public class Main
{
   public static void main(String[] args) {
      Class<Long> c = Long.TYPE;
      Class<Long> c1 = long.class;
   }
}

Then, using javap -c Main:

c:\dev\src\misc>javap -c Main
Compiled from "Main.java"
public class Main {
  public Main();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":
()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/Long.TYPE:Ljava/lang/Class;
       3: astore_1
       4: getstatic     #2                  // Field java/lang/Long.TYPE:Ljava/lang/Class;
       7: astore_2
       8: return
}
like image 87
rgettman Avatar answered Sep 20 '22 15:09

rgettman