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.
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.
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With