在 Java 中将对象强制转换为 Long
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38116158/
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
Cast an Object To Long in Java
提问by Dat Tan Nguyen
I am trying to convert a Object type to Long type in Java and I got as:
我正在尝试在 Java 中将 Object 类型转换为 Long 类型,结果如下:
public static Long castObjectToLong(Object object) {
return ((Long)object).longValue();
When I run, it throws ClassCastException
当我运行时,它抛出 ClassCastException
回答by sawyinwaimon
when you write return ((Long)object).longValue();
causes ClassCastException because Object is not Long.That I mean is if Object o = new Long(),then you can make cast ((Long)object).This is the example I wrote is just like:
当你写的时候 return ((Long)object).longValue();
会导致 ClassCastException 因为 Object is not Long。我的意思是如果 Object o = new Long(), then you can make cast ((Long)object). 这是我写的例子就像:
public class Test {
public static void main(String args[]){
System.out.println(convertToLong(10));
}
public static Long convertToLong(Object o){
String stringToConvert = String.valueOf(o);
Long convertedLong = Long.parseLong(stringToConvert);
return convertedLong;
}
}
I convert Object to String first.Then String to Long.Please see this code is ok to use for you or not.
我先将 Object 转换为 String。然后将 String 转换为 Long。请查看此代码是否适合您。
回答by Satish Karuturi
you can try like this:
你可以这样尝试:
public class HelloWorld{
public static Long castObjectToLong(Object object) {
return Long.parseLong(object.toString());
}
public static void main(String []args){
System.out.println("Hello World");
Object object=1234;
System.out.println(castObjectToLong(object));
}
}
回答by Luan Vo
The following code may help:
以下代码可能会有所帮助:
public class CastObjectToLong {
public Long castLongObject(Object object) {
Long result = 0l;
try {
if (object instanceof Long)
result = ((Long) object).longValue();
else if (object instanceof Integer) {
result = ((Integer) object).longValue();
} else if (object instanceof String) {
result = Long.valueOf((String) object);
}
System.out.println(result);
} catch (Exception e) {
System.out.println("============= cannot cast");
// do something
}
return result;
}
public static void main(String... args) {
CastObjectToLong castObjectToLong = new CastObjectToLong();
Object object1 = 12; // Integer
Object object2 = "12"; // String
Object object3 = 12l; // String
Object object4 = "abc"; // String
castObjectToLong.castLongObject(object1);
castObjectToLong.castLongObject(object2);
castObjectToLong.castLongObject(object3);
castObjectToLong.castLongObject(object4); // exception here
}
}
Outputs:
输出:
12
12
12
12
12
12
============= cannot cast
============== 无法投射
回答by Mithilesh Tipkari
You got ClassCastException
because may be you were trying to convert java.lang.Integer
into java.lang.Long
which cannot be done directly.
你得到了,ClassCastException
因为可能是你试图转换java.lang.Integer
成java.lang.Long
无法直接完成的。
Try to convert object to String and then pass it as an argument to constructor of Long
尝试将对象转换为字符串,然后将其作为参数传递给构造函数 Long
public static Long castObjectToLong(Object object) {
return new Long(object.toString());
}