0

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?

Hubert Bratek
  • 894
  • 10
  • 30

1 Answers1

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());
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • 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? – Hubert Bratek Aug 19 '18 at 16:49
  • `(long) yourInt` maybe.. `Ex: (long) 2` – Roshana Pitigala Aug 19 '18 at 16:50
  • 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? – Hubert Bratek Aug 19 '18 at 16:52
  • @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`). – Bernhard Barker Aug 19 '18 at 17:03
  • Your update still doesn't help as It requires if statement for integer.(check question update) – Hubert Bratek Aug 19 '18 at 17:18
  • 2
    @Ajris Every [`Number`](https://docs.oracle.com/javase/8/docs/api/java/lang/Number.html) subclass (`Long`, `Integer`, `Short`, `Byte`, etc.) has a `longValue` method - you shouldn't need an if-statement for `Integer` specifically. – Bernhard Barker Aug 19 '18 at 17:26
  • I'd suggest removing the second part of your answer. `Long.class.cast` is a no-op (i.e. it does nothing) in those cases, because the input is already an (autoboxed) `Long` (the first part of your answer does the same just without the `cast`). – Bernhard Barker Aug 19 '18 at 17:32
  • Hmmm, all right, so the only if that I have to write is about checking whether Number class is assignable from my object type right? And then just convert it using TYPEvalue method – Hubert Bratek Aug 19 '18 at 17:39
  • @Ajris Yes, probably. – Bernhard Barker Aug 19 '18 at 18:21