I am trying to convert a Object type to Long type in Java and I got as:
public static Long castObjectToLong(Object object) {
return ((Long)object).longValue();
When I run, it throws ClassCastException
I am trying to convert a Object type to Long type in Java and I got as:
public static Long castObjectToLong(Object object) {
return ((Long)object).longValue();
When I run, it throws ClassCastException
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:
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.
You got ClassCastException
because may be you were trying to convert java.lang.Integer
into java.lang.Long
which cannot be done directly.
Try to convert object to String and then pass it as an argument to constructor of Long
public static Long castObjectToLong(Object object) {
return new Long(object.toString());
}
Long.valueOf(object.toString())
instead –
Cespitose 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));
}
}
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
============= cannot cast
result = ((Long) object).longValue()
it can be just result = (Long) object;
. –
Cram In your code, ClassCastException is being thrown because Object is not Long. There is also a probability of encountering a NullPointerException, in case your object is null.
Hence the best solution would be to do a null check on your object , then convert it to String using either String.valueOf or toString() and then parse it to Long.
The difference between String.valueOf(object) and object.toString() won't matter here, as the former returns null when the object is null where as the latter throws NullpointerException.
I love to use BigDecimal
. Simple and error-free. Even if your object contains a decimal it will handle it.
long l = new BigDecimal(String.valueOf(object).setScale(0).longValue());
This is because you are returning long
value instead of Long.longValue()
method which you have used will give long value only but you don't want primitive data type value instead you want Long
so simply casting it will work!!
i.e return (Long)object
instead of ((Long)object).longValue();
The Simplest way to do the conversion from object to Long is:
public class Test {
public static void main(String[] args) {
Object a=15;
System.out.println(Long.parseLong(a.toString()));
}
}
output:15
© 2022 - 2024 — McMap. All rights reserved.
Object
toLong
? – Tartonpublic static long castObjectToLong( ...
– Junitajuniusit does not work well
? – Hiding