Cast an Object To Long in Java

We Are Going To Discuss About Cast an Object To Long in Java. So lets Start this Java Article.

Cast an Object To Long in Java

Advertisements
  1. Cast an Object To Long in Java

    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:

  2. Cast an Object To Long in Java

    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:

Solution 1

Advertisements

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.

Original Author sawyinwaimon Of This Content

Solution 2

Advertisements

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

Original Author Mithilesh Tipkari Of This Content

Solution 3

Advertisements

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

Original Author ROOT Of This Content

Solution 4

Advertisements

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

Original Author Luan Vo Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment