I'm trying to concatenate/merge two Integer[] array and store it in an other Integer[]. But I'm getting bellow error at run time.
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Object; and [Ljava.lang.Integer; are in module java.base of loader 'bootstrap') at AllInOnePack.MainClass.AllInOne.main(AllInOne.java:61)
My code is like this.
Main class.java
static Integer[] hardCodeValus = {1,2,3,4};
static Integer[] userValue = {1,2,3,4};
concatArray = new Integer[hardCodeValus.length+userValue.length];
concatArray = (Integer[]) StreamsFunc.concatenate(hardCodeValus, userValue);
StreamsFunc.java
public static <T> Object[] concatenate(T[] hardCodeValus, T[] userValue)
{
return Stream.concat(Arrays.stream(hardCodeValus), Arrays.stream(userValue)).toArray();
}
During run time I'm getting the error. Why this is not found in compile time then?
final String string = (String) new Object();
will compile even though the cast makes no sense.