Java 中如何将 int 转换为 Long?

发布于 2024-08-02 06:58:27 字数 560 浏览 1 评论 0原文

我一直发现这里和 Google 的人们在从 long 转换为 int 时遇到了麻烦,而不是相反。但我确信我不是唯一一个在从 int 转换为 Long 之前遇到过这种情况的人。

我发现的唯一其他答案是“首先将其设置为 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);
}

正如您可以想象的那样,我有点困惑,因为某些内容,我一直使用 int以 ArrayList 形式传入,而我为其存储此信息的实体需要长整型序列号。

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.

The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question.

I initially tried casting but I get a "Cannot cast from int to 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

安静被遗忘 2024-08-09 06:58:27

请注意,转换为 long 和转换为 Long 之间存在差异。如果您转换为long(原始值),那么它应该自动装箱为Long(包装它的引用类型)。

您也可以使用 new 创建 Long 的实例,并使用 int 值对其进行初始化。

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).

You could alternatively use new to create an instance of Long, initializing it with the int value.

窝囊感情。 2024-08-09 06:58:27

使用以下内容: Long.valueOf(int);

Use the following: Long.valueOf(int);.

空气里的味道 2024-08-09 06:58:27

如果您已经将 int 输入为 Integer,则可以执行以下操作:

Integer y = 1;
long x = y.longValue();

If you already have the int typed as an Integer you can do this:

Integer y = 1;
long x = y.longValue();
如痴如狂 2024-08-09 06:58:27

使用

new Long(your_integer);

Long.valueOf(your_integer);

use

new Long(your_integer);

or

Long.valueOf(your_integer);
苍风燃霜 2024-08-09 06:58:27
 1,new Long(intValue);
 2,Long.valueOf(intValue);
 1,new Long(intValue);
 2,Long.valueOf(intValue);
┾廆蒐ゝ 2024-08-09 06:58:27

怎么样

int myInt = 88;

// 不会编译

Long myLong = myInt;

// 编译,并保留 int 的非 NULL 精神。最好的演员就是完全没有演员。当然,您的用例可能需要 Long 值和可能的 NULL 值。但是,如果 int 或其他 long 是您唯一的输入,并且您的方法可以修改,我建议采用这种方法。

long myLong = myInt;

// 编译是最有效的方法,并且清楚地表明源值是并且永远不会是 NULL。

Long myLong = (long) myInt;

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.

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;
西瓜 2024-08-09 06:58:27

在Java中你可以这样做:

 int myInt=4;
 Long myLong= new Long(myInt);

在你的情况下它将是:

content.setSequence(new Long(i));

In Java you can do:

 int myInt=4;
 Long myLong= new Long(myInt);

in your case it would be:

content.setSequence(new Long(i));
狠疯拽 2024-08-09 06:58:27

我有这个小玩具,它也处理非通用接口。
如果输入错误,我可以接受抛出 ClassCastException (好的并且很高兴)

public class TypeUtil {
    public static long castToLong(Object o) {
        Number n = (Number) o;
        return n.longValue();
    }
}

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)

public class TypeUtil {
    public static long castToLong(Object o) {
        Number n = (Number) o;
        return n.longValue();
    }
}
熟人话多 2024-08-09 06:58:27

我们将通过使用 Number 引用来获取长值。

public static long toLong(Number number){
    return number.longValue();
}

它适用于所有数字类型,这是一个测试:

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

}

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

}
野鹿林 2024-08-09 06:58:27

我为此遇到了很大的麻烦。我只是想:

thisBill.IntervalCount = jPaidCountSpinner.getValue();

其中 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;
}

这似乎可以解决问题。没有任何简单的铸造,上述解决方案都不适合我。非常令人沮丧。

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:

    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.

故人的歌 2024-08-09 06:58:27
 //Suppose you have int and you wan to convert it to Long
 int i=78;
 //convert to Long
 Long l=Long.valueOf(i)
 //Suppose you have int and you wan to convert it to Long
 int i=78;
 //convert to Long
 Long l=Long.valueOf(i)
星星的軌跡 2024-08-09 06:58:27

Android Studio lint 检查建议:删除不必要的装箱:所以,拆箱是:

public  static  long  integerToLong (int minute ){
    int delay = minute*1000;
    long diff = (long) delay;
    return  diff ; 
}

Suggested From Android Studio lint check : Remove Unnecessary boxing : So, unboxing is :

public  static  long  integerToLong (int minute ){
    int delay = minute*1000;
    long diff = (long) delay;
    return  diff ; 
}
篱下浅笙歌 2024-08-09 06:58:27

除了这里建议的其他方法之外,还可以尝试下面的代码。

(long)intValue

原始到原始。

Apart from the other ways suggested here, one can try the below code as well.

(long)intValue

primitive to primitive.

我要还你自由 2024-08-09 06:58:27

只要只有方法 Long.valueOf(long),在使用 的情况下,就会隐式地从 int 转换为 long >Long.valueOf(intValue)

更明确的方法是

Integer.valueOf(intValue).longValue()

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).

The more clear way to do this is

Integer.valueOf(intValue).longValue()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文