7

As we know int is a primitive data type and cannot be null. In my program i want to do a condition check like int i != "" but it says operator != cannot be applied to int,null.

Any solution?

EDIT:

I already know that it cannot hold these two things i was trying to present my requiremnet for checking the parameter i am getting from another server if it has recived some value or if its empty.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Mohsin Sheikh Khalid
  • 3,914
  • 5
  • 20
  • 22

15 Answers15

7

You can use the Integer wrapper class for such comparisons. Otherwise the primitive int, if as a class field, defaults to 0 if not specified otherwise.

dimitrisli
  • 20,895
  • 12
  • 59
  • 63
6

compiler never speak un-truth .

int can't hold so there is no need to check for "" or null

jmj
  • 237,923
  • 42
  • 401
  • 438
  • Thank you but i already know that it cannot hold these two things i was trying to present my requiremnet for checking the parameter i am getting from another server if it has recived some value or if its empty. – Mohsin Sheikh Khalid Nov 10 '10 at 08:01
  • 1
    @Mohsin if server is supposed to return int specifically than it can't return these values, but if its written to return String than you are converting it to int than checking is OK. – jmj Nov 10 '10 at 08:06
4

You should check "i!=0", "i!=-1" or any other which better suits your requirements.

Mudassir
  • 13,031
  • 8
  • 59
  • 87
4

The only two possibilities I can think of:

  • Depending on the valid range of values for your variable, initialize it with a value like "0" or "-1" to mark it as unused/empty.

  • You could use java.lang.Integer instead and only instantiate it passing the int value, if you actually use it. However, if your variable changes a lot, this will lead to a lot of object creations, as the Integer objects are immutable by default. Thus, every change of value leads to a new Integer object that needs to be created.

Christoph Metzendorf
  • 7,968
  • 2
  • 31
  • 28
4

If you need/expect the "null-state" for integer values then you'll have to use the wrapper class:

Integer maybeNull = getValueFromDatabase();
if (mybeNull != null) {
  int i = maybeNull;  // outboxing, works with Java 1.5+
} else {
  // column value on database was `NULL`
}
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

You want to see if a value was submitted for the parameter, but you're putting it in a primitive type. That's not possible.

I'm assuming you're using some kind of framework to communicate with the other server. If the value you receive from the framework is already an int, it will always have a value. The framework will have defaulted it for you (the value will probably be zero then).

You need a framework that will give you an Integer, not an int. In that case, it will give you null if no value was given.

There is no way to check if an int has no value on your side, since an int cannot have 'no value'.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
2

int data type holds an integer, so you can't compare it with a string or null. This is a compiler error

DaJackal
  • 2,085
  • 4
  • 32
  • 48
2

If you have a value different than the integer range, then use the wrapper type Integer - it has an additional value - null. So you will be able to do: if (i != null) {..}

However it is rarely advisable to use a wrapper type just for the sake of having a special null value. You can use Integer.MAX_VALUE as a default value.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
2
  • int accepts a range of whole number values
  • "" is a String, not an int.
  • null is used for references not ints (or other primitives).

The real question is why do you want to do such a comparison?

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
2

Create a Nullable class a la C#.

class Nullable<A> {
  private A value = null;

  private Nullable(A value) {
    this.value = value;
  }

  public static <A> Nullable<A> withValue(A value) {
    return new Nullable<A>(value);
  }

  public static <A> Nullable<A> empty() {
    return new Nullable<A>(null);
  }

  public boolean hasValue() {
    return value != null;
  }

  public A getValue() {
    return value;
  }

  public void setValue(A value) {
    this.value = value;
  }      

  // Implement toString, equals, hashCode etc.
}

and then use it as:

Nullable<Integer> ix = Nullable.<Integer>empty();
if(ix.hasValue()) {
  int i = ix.getValue();
  // do operations with i 
} else {
  // handle the 'null' case
}
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
1

Thank you all for your good insights, they all were infromative and helpful. What was happening in my scenario was that the input i was getting was a string whihc unfortunately i did not know yet so now i just compare that String to "" and check for the empty condition and if there is a value i convert it back to int. :)

Mohsin Sheikh Khalid
  • 3,914
  • 5
  • 20
  • 22
  • 1
    When so many helpful answers were provided why did you accept your own answer instead of one of the others? – WJS Aug 07 '22 at 19:31
1

if you facing problem of < operator != not applied to long and int > Simply using long on your right side ie value!= 1L thats it..

Ranajit Sawant
  • 181
  • 1
  • 6
1

cast to int/long help me on .jsp - page:

<%if ((long)request.getAttribute("amount") != 0) {
      out.println(request.getAttribute("amount"));}
  else {out.println(payments.getAmount());}%>
naut92
  • 103
  • 1
  • 13
0

Try to use Integer instead of int

Integer value = 2; 
value 
0
if(Long.valueOf(your number) != null){
  some analyze
}
vimuth
  • 5,064
  • 33
  • 79
  • 116
vhg2901
  • 11
  • 4
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 07 '22 at 11:30