1

I have simple question. I already know that in Java you can't do sth like:

Long.class.cast(and Integer here)

but I am just curious why? In the opposite way you can easily get into overflow, but this way I can't find anything bad which can happen. Can anyone tell me what is wrong in using casting that way?

EDIT->

So what I am trying to do. I have already wrote a converter which converts me from my provided input(it is a Map) to my model. I wanted to write 1 converter for every model which I specify. Currently, it works for objects inside it(such as other models which I provided), strings, and the same types(for example I have got an integer and I provide an integer). What I am trying to do, I want to 'update' my converter to work in such cases like: in my model I have a field of type Long, and I provided Integer. So I want it to convert Integer to Long, however, I don't want to make if only for that case(because in such situation for example converting from Short to Long would be another if). Is it possible?

7
  • Integer integer = 1; System.out.println(Long.class.cast(integer)); It throws exception, and I am just wondering why Commented Aug 19, 2018 at 16:42
  • it is just done different way: Long l = new Long(10i); Commented Aug 19, 2018 at 16:43
  • 3
    @HovercraftFullOfEels Class#cast(Object)). Commented Aug 19, 2018 at 16:46
  • 1
    @Dukeling: thank you for that Commented Aug 19, 2018 at 16:54
  • @Ajris What are you trying to do? Based on your comment it looks like you are trying to do something else. This is known as a "XY problem", so you might want to write something about your "X" problem. Commented Aug 19, 2018 at 16:58

1 Answer 1

2

Because Object.cast(Object obj) is not just for numbers. It's used for type convertion.

ClassCastException will be throws if the object is not null and is not assignable to the type Long.

You can just use this:

Integer integer = 1;

Long l = integer.longValue();

Or you can call longValue on Long or Integer, then use Long.class.cast:

Integer integer = 1;
Long longType = 1L;

Long l = Long.class.cast(longType.longValue());
l = Long.class.cast(integer.longValue());
Sign up to request clarification or add additional context in comments.

9 Comments

Yea, but when I use reflection and I don't want to use ifs for casting integer to long, is it possible to do it another way?
(long) yourInt maybe.. Ex: (long) 2
I don't want to use ifs, co for example if I use casting, it works for converting some of my classes to another ones. And if I provide integer, it wouldn't convert it to long because of this exception. And I don't want to check every time whether I provided integer or another type, just want to have one solution which also works for Integer and Long. Is it possible?
@Ajris An int value can be assigned to a long variable without (explicit) casting. Using primitives would probably be better, unless you have a specific need to use classes (or your input is an Object).
@Ajris Every Number subclass (Long, Integer, Short, Byte, etc.) has a longValue method - you shouldn't need an if-statement for Integer specifically.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.