I keep finding both on here and Google people having troubles going from long
to int
and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int
to Long
.
我一直在这里发现谷歌的人有麻烦从长到整数,而不是反过来。然而我确信我不是唯一一个在从int到Long之前遇到这种情况的人。
The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question.
我发现的唯一的其他答案是“只要把它放在第一位就好”,这并不能解决问题。
Can someone help me out here? I initially tried casting but I get a "Cannot cast from int to Long
"
有人能帮我吗?我最初试过施法,但我得到了"不能从int到Long"
for (int i = 0; i < myArrayList.size(); ++i ) {
content = new Content();
content.setDescription(myArrayList.get(i));
content.setSequence((Long) i);
session.save(content);
}
As you can imagine I'm a little perplexed, I'm stuck using int since some content is coming in as an ArrayList and the entity for which I'm storing this info requires the sequence number as a Long.
您可以想象我有点困惑,我被困在使用int,因为有些内容作为ArrayList进入,我存储这个信息的实体需要一个长序列号。
Thanks!
谢谢!
12 个解决方案
#1
208
Note that there is a difference between a cast to long
and a cast to Long
. If you cast to long
(a primitive value) then it should be automatically boxed to a Long
(the reference type that wraps it).
注意,在投长和投长之间是有区别的。如果您将其转换为long(一个原始值),那么它应该被自动地装入一个long(包含它的引用类型)。
You could alternatively use new
to create an instance of Long
, initializing it with the int
value.
您也可以使用new创建Long的实例,用int值初始化它。
#3
13
If you already have the int typed as an Integer you can do this:
如果您已经将int类型设置为整数,您可以这样做:
Integer y = 1;
long x = y.longValue();
#4
8
use
使用
new Long(your_integer);
or
或
Long.valueOf(your_integer);
#5
5
I have this little toy, that also deals with non generic interfaces. I'm OK with it throwing a ClassCastException if feed wrong (OK and happy)
我有一个小玩具,它也处理非通用接口。如果输入错误,我就会抛出ClassCastException (OK和happy)
public class TypeUtil {
public static long castToLong(Object o) {
Number n = (Number) o;
return n.longValue();
}
}
#6
4
In Java you can do:
在Java中你可以做到:
int myInt=4;
Long myLong= new Long(myInt);
in your case it would be:
就你而言,它将是:
content.setSequence(new Long(i));
#7
2
We shall get the long value by using Number
reference.
我们将使用数字引用来获取长值。
public static long toLong(Number number){
return number.longValue();
}
It works for all number types, here is a test:
它适用于所有数字类型,这里有一个测试:
public static void testToLong() throws Exception {
assertEquals(0l, toLong(0)); // an int
assertEquals(0l, toLong((short)0)); // a short
assertEquals(0l, toLong(0l)); // a long
assertEquals(0l, toLong((long) 0)); // another long
assertEquals(0l, toLong(0.0f)); // a float
assertEquals(0l, toLong(0.0)); // a double
}
#8
2
How About
如何
int myInt = 88;
// Will not compile
/ /将不会编译
Long myLong = myInt;
// Compiles, and retains the non-NULL spirit of int. The best cast is no cast at all. Of course, your use case may require Long and possible NULL values. But if the int, or other longs are your only input, and your method can be modified, I would suggest this approach.
//编译,并保留int的非空精神。最好的施法是完全不施法。当然,您的用例可能需要长时间和可能的空值。但是如果int或其他long类型是您唯一的输入,并且您的方法可以被修改,我建议采用这种方法。
long myLong = myInt;
// Compiles, is the most efficient way, and makes it clear that the source value, is and will never be NULL.
//编译,是最有效的方法,并且表明源值是并且永远不会为空。
Long myLong = (long) myInt;
#9
1
//Suppose you have int and you wan to convert it to Long
int i=78;
//convert to Long
Long l=Long.valueOf(i)
#10
0
I had a great deal of trouble with this. I just wanted to:
我遇到了很多麻烦。我只是想:
thisBill.IntervalCount = jPaidCountSpinner.getValue();
Where IntervalCount is a Long, and the JSpinner was set to return a Long. Eventually I had to write this function:
其中IntervalCount为Long,而JSpinner设置为返回Long。最后我必须写出这个函数:
public static final Long getLong(Object obj) throws IllegalArgumentException {
Long rv;
if((obj.getClass() == Integer.class) || (obj.getClass() == Long.class) || (obj.getClass() == Double.class)) {
rv = Long.parseLong(obj.toString());
}
else if((obj.getClass() == int.class) || (obj.getClass() == long.class) || (obj.getClass() == double.class)) {
rv = (Long) obj;
}
else if(obj.getClass() == String.class) {
rv = Long.parseLong(obj.toString());
}
else {
throw new IllegalArgumentException("getLong: type " + obj.getClass() + " = \"" + obj.toString() + "\" unaccounted for");
}
return rv;
}
which seems to do the trick. No amount of simple casting, none of the above solutions worked for me. Very frustrating.
这似乎很管用。没有多少简单的铸造,以上的解决方案都不适合我。非常令人沮丧。
#11
0
As soon as there is only method Long.valueOf(long)
, cast from int
to long
will be done implicitly in case of using Long.valueOf(intValue)
.
只要只有long . valueof (long)方法,在使用long . valueof (intValue)时,将隐式地从int类型转换为long类型。
The more clear way to do this is
更清晰的方法是
Integer.valueOf(intValue).longValue()
#12
0
1,new Long(intValue);
2,Long.valueOf(intValue);
#1
208
Note that there is a difference between a cast to long
and a cast to Long
. If you cast to long
(a primitive value) then it should be automatically boxed to a Long
(the reference type that wraps it).
注意,在投长和投长之间是有区别的。如果您将其转换为long(一个原始值),那么它应该被自动地装入一个long(包含它的引用类型)。
You could alternatively use new
to create an instance of Long
, initializing it with the int
value.
您也可以使用new创建Long的实例,用int值初始化它。
#2
#3
13
If you already have the int typed as an Integer you can do this:
如果您已经将int类型设置为整数,您可以这样做:
Integer y = 1;
long x = y.longValue();
#4
8
use
使用
new Long(your_integer);
or
或
Long.valueOf(your_integer);
#5
5
I have this little toy, that also deals with non generic interfaces. I'm OK with it throwing a ClassCastException if feed wrong (OK and happy)
我有一个小玩具,它也处理非通用接口。如果输入错误,我就会抛出ClassCastException (OK和happy)
public class TypeUtil {
public static long castToLong(Object o) {
Number n = (Number) o;
return n.longValue();
}
}
#6
4
In Java you can do:
在Java中你可以做到:
int myInt=4;
Long myLong= new Long(myInt);
in your case it would be:
就你而言,它将是:
content.setSequence(new Long(i));
#7
2
We shall get the long value by using Number
reference.
我们将使用数字引用来获取长值。
public static long toLong(Number number){
return number.longValue();
}
It works for all number types, here is a test:
它适用于所有数字类型,这里有一个测试:
public static void testToLong() throws Exception {
assertEquals(0l, toLong(0)); // an int
assertEquals(0l, toLong((short)0)); // a short
assertEquals(0l, toLong(0l)); // a long
assertEquals(0l, toLong((long) 0)); // another long
assertEquals(0l, toLong(0.0f)); // a float
assertEquals(0l, toLong(0.0)); // a double
}
#8
2
How About
如何
int myInt = 88;
// Will not compile
/ /将不会编译
Long myLong = myInt;
// Compiles, and retains the non-NULL spirit of int. The best cast is no cast at all. Of course, your use case may require Long and possible NULL values. But if the int, or other longs are your only input, and your method can be modified, I would suggest this approach.
//编译,并保留int的非空精神。最好的施法是完全不施法。当然,您的用例可能需要长时间和可能的空值。但是如果int或其他long类型是您唯一的输入,并且您的方法可以被修改,我建议采用这种方法。
long myLong = myInt;
// Compiles, is the most efficient way, and makes it clear that the source value, is and will never be NULL.
//编译,是最有效的方法,并且表明源值是并且永远不会为空。
Long myLong = (long) myInt;
#9
1
//Suppose you have int and you wan to convert it to Long
int i=78;
//convert to Long
Long l=Long.valueOf(i)
#10
0
I had a great deal of trouble with this. I just wanted to:
我遇到了很多麻烦。我只是想:
thisBill.IntervalCount = jPaidCountSpinner.getValue();
Where IntervalCount is a Long, and the JSpinner was set to return a Long. Eventually I had to write this function:
其中IntervalCount为Long,而JSpinner设置为返回Long。最后我必须写出这个函数:
public static final Long getLong(Object obj) throws IllegalArgumentException {
Long rv;
if((obj.getClass() == Integer.class) || (obj.getClass() == Long.class) || (obj.getClass() == Double.class)) {
rv = Long.parseLong(obj.toString());
}
else if((obj.getClass() == int.class) || (obj.getClass() == long.class) || (obj.getClass() == double.class)) {
rv = (Long) obj;
}
else if(obj.getClass() == String.class) {
rv = Long.parseLong(obj.toString());
}
else {
throw new IllegalArgumentException("getLong: type " + obj.getClass() + " = \"" + obj.toString() + "\" unaccounted for");
}
return rv;
}
which seems to do the trick. No amount of simple casting, none of the above solutions worked for me. Very frustrating.
这似乎很管用。没有多少简单的铸造,以上的解决方案都不适合我。非常令人沮丧。
#11
0
As soon as there is only method Long.valueOf(long)
, cast from int
to long
will be done implicitly in case of using Long.valueOf(intValue)
.
只要只有long . valueof (long)方法,在使用long . valueof (intValue)时,将隐式地从int类型转换为long类型。
The more clear way to do this is
更清晰的方法是
Integer.valueOf(intValue).longValue()
#12
0
1,new Long(intValue);
2,Long.valueOf(intValue);