如何处理 Java 中的“int i != ""” 或 “int i != null” 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4142239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle “int i != ""” or “int i != null” statements in Java?
提问by Mohsin Sheikh Khalid
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.
我们知道 int 是一种原始数据类型,不能为空。在我的程序中,我想做一个条件检查,比如 int i != "" 但它说 operator != 不能应用于 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.
我已经知道它不能容纳这两件事,我试图提出我的 requiremnet 来检查我从另一台服务器获得的参数,如果它已经接收到某个值或者它是否为空。
采纳答案by Mohsin Sheikh Khalid
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. :)
谢谢大家的好见解,他们都提供了信息和帮助。在我的场景中发生的事情是,我得到的输入是一个字符串,不幸的是我还不知道,所以现在我只是将该字符串与“”进行比较并检查空条件,如果有一个值,我将其转换回内部 :)
回答by Jigar Joshi
compiler never speak un-truth .
编译器从不说不真实的。
int can't hold so there is no need to check for ""
or null
int 不能保持所以没有必要检查 ""
或null
回答by Mudassir
You should check "i!=0", "i!=-1" or any other which better suits your requirements.
您应该检查 "i!=0"、"i!=-1" 或任何其他更符合您要求的内容。
回答by DaHymanal
int data type holds an integer, so you can't compare it with a string or null. This is a compiler error
int 数据类型保存一个整数,因此您不能将其与字符串或空值进行比较。这是一个编译器错误
回答by Bozho
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) {..}
如果您有一个不同于整数范围的值,则使用包装器类型Integer
- 它有一个附加值 - null
。所以你将能够做到: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.
然而,仅仅为了具有特殊null
值而使用包装器类型很少是可取的。您可以将其Integer.MAX_VALUE
用作默认值。
回答by Christoph Metzendorf
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.
根据变量的有效值范围,使用“0”或“-1”之类的值对其进行初始化以将其标记为未使用/空。
您可以改用 java.lang.Integer 并且仅实例化它并传递 int 值(如果您实际使用它)。但是,如果您的变量更改很多,这将导致创建大量对象,因为默认情况下 Integer 对象是不可变的。因此,每次更改值都会导致需要创建一个新的 Integer 对象。
回答by TofuBeer
- int accepts a range of whole number values
- "" is a String, not an int.
- null is used for references not ints (or other primitives).
- int 接受一系列整数值
- "" 是一个字符串,而不是一个整数。
- null 用于引用而不是整数(或其他原语)。
The real question is why do you want to do such a comparison?
真正的问题是你为什么要做这样的比较?
回答by Andreas Dolk
If you need/expect the "null
-state" for integer values then you'll have to use the wrapper class:
如果您需要/期望null
整数值的“ -state”,那么您将不得不使用包装类:
Integer maybeNull = getValueFromDatabase();
if (mybeNull != null) {
int i = maybeNull; // outboxing, works with Java 1.5+
} else {
// column value on database was `NULL`
}
回答by missingfaktor
Create a Nullable
class a la C#.
创建一个Nullable
类,如 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
}
回答by dimitrisli
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.
您可以使用 Integer 包装类进行此类比较。否则原始 int,如果作为类字段,如果没有另外指定,则默认为 0。
回答by Joeri Hendrickx
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).
我假设您正在使用某种框架与其他服务器进行通信。如果您从框架接收到的值已经是一个 int,它将始终具有一个值。该框架将为您默认它(然后该值可能为零)。
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.
你需要一个框架来给你一个整数,而不是一个整数。在这种情况下,如果没有给出值,它会给你 null。
There is no way to check if an int has no value on your side, since an int cannot have 'no value'.
没有办法检查 int 是否对您没有价值,因为 int 不能有“无价值”。