Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning null in a method whose signature says return int?

Tags:

java

null

return

public int pollDecrementHigherKey(int x) {             int savedKey, savedValue;             if (this.higherKey(x) == null) {                 return null;  // COMPILE-TIME ERROR             }             else if (this.get(this.higherKey(x)) > 1) {                         savedKey = this.higherKey(x);                 savedValue = this.get(this.higherKey(x)) - 1;                 this.remove(savedKey);                 this.put(savedKey, savedValue);                 return savedKey;             }             else {                 savedKey = this.higherKey(x);                 this.remove(savedKey);                 return savedKey;             }         } 

The method lies within a class that is an extension of TreeMap, if that makes any difference... Any ideas why I can't return null here?

like image 846
r123454321 Avatar asked Jun 20 '13 19:06

r123454321


People also ask

Can you return null in int method?

int is a primitive, null is not a value that it can take on. You could change the method return type to return java. lang. Integer and then you can return null, and existing code that returns int will get autoboxed.

Can you return null in a method?

Then programmers that call your method know that they should check if the return value is null: ? Hi there, Yes it can be considered a valid return technique, it just needs to be clearly documented in the Javadoc for that method e.g. "Returns x if one exists else null".

How do you return a null to an int in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.

Why is my method returning null?

The problem appears when Get is invoked on a cache which doesn't contain the item with specified key value. In that case, Get just returns the default value for the item type T – in reference types that means null. It is obvious that the client must guard from receiving null result back from the method call.


2 Answers

int is a primitive, null is not a value that it can take on. You could change the method return type to return java.lang.Integer and then you can return null, and existing code that returns int will get autoboxed.

Nulls are assigned only to reference types, it means the reference doesn't point to anything. Primitives are not reference types, they are values, so they are never set to null.

Using the object wrapper java.lang.Integer as the return value means you are passing back an Object and the object reference can be null.

like image 149
Nathan Hughes Avatar answered Sep 21 '22 17:09

Nathan Hughes


int is a primitive data type . It is not a reference variable which can take null values . You need to change the method return type to Integer wrapper class .

like image 34
AllTooSir Avatar answered Sep 20 '22 17:09

AllTooSir