Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass null to method that expects long or int?

Tags:

java

Could be dumb question, but how can I pass null to method which takes long or int?

Example:

TestClass{   public void iTakeLong(long id);   public void iTakeInt(int id); } 

now how can i pass null to both methos:

TestClass testClass = new TestClass(); testClass.iTakeLong(null); // Compilation error : expected long, got null testClass.iTakeInt(null);   // Compilation error : expected int, got null 

Thoughts, suggestions?

like image 276
Rachel Avatar asked Apr 30 '12 18:04

Rachel


People also ask

How do you pass int value as null?

Int? a=null; Nullable<int>a=null; Null value.

Can you pass null to a method?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.

Can long be assigned null?

If you have declared the data type of Plate number as long, then obviously its initial value will be '0' and not 'null'. So, if you try to increment, it will run without any issues.

Can null be assigned to int?

Assigning Null to Variables null can only be assigned to reference type, you cannot assign null to primitive variables e.g. int, double, float, or boolean.


1 Answers

The problem is that int and long are primitives. You can't pass null to a primitive value.

You can certainly use the wrapper classes Integer and Long instead of long and int in your method signature.

like image 82
Makoto Avatar answered Sep 30 '22 11:09

Makoto