如何在 Java 中从 int 转换为 Long?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1302605/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:01:10  来源:igfitidea点击:

How do I convert from int to Long in Java?

javacastingintlong-integer

提问by Ghosty

I keep finding both on here and Google people having troubles going from longto intand not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from intto Long.

我一直发现在这里和谷歌的人都有麻烦longint而不是相反。然而,我确信我不是唯一一个在从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.

我发现的唯一其他答案是“首先将其设置为 Long”,这确实没有解决问题。

I initially tried casting but I get a "Cannot cast from int to Long"

我最初尝试铸造,但我得到一个“ 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 intsince some content is coming in as an ArrayListand the entity for which I'm storing this info requires the sequence number as a Long.

正如您可以想象的那样,我有点困惑,我一直在使用,int因为一些内容是作为一个ArrayList输入的,而我存储此信息的实体需要将序列号作为一个 Long。

采纳答案by Daniel Earwicker

Note that there is a difference between a cast to longand 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).

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

You could alternatively use newto create an instance of Long, initializing it with the intvalue.

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

回答by serg

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

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

回答by saret

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

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

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

回答by MaskedCoder

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.

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

回答by Maxim Veksler

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(好的和快乐)

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

回答by cloudy_weather

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

回答by Thamme Gowda

We shall get the long value by using Numberreference.

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

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

}

回答by User User

use


new Long(your_integer);

or

或者

Long.valueOf(your_integer);

回答by Steven Spungin

How About

怎么样

int myInt = 88;

// Will not compile

// 不会编译

Long myLong = myInt;

// Compiles, and retains the non-NULL spirit of int. The best castis 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 的非 NULL 精神。最好的演员根本没有演员。当然,您的用例可能需要 Long 和可能的 NULL 值。但是,如果 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.

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

Long myLong = (long) myInt;

回答by Virendra Singh

 //Suppose you have int and you wan to convert it to Long
 int i=78;
 //convert to Long
 Long l=Long.valueOf(i)