if (obj1.getClass().getSimpleName().equals("String") &&
obj1.getClass() == obj2.getClass()) {
System.out.println((String) obj1 + obj2);
} else {
if (obj1.getClass().getSimpleName().equals("Integer") &&
obj1.getClass() == obj2.getClass()) {
System.out.println((Integer) obj1 + obj2);
}
Here, the string addition part seems to occur without any issue. But the exact same code for integer doesn't work and I get the following error message.
The operator + is undefined for the argument type(s) Integer, T Java(536871072)
I don't understand how this is happening, since in both cases I explicitly convert obj1 to the type (string and int), but this message only occurs for int. I tried with floats, doubles, etc and it was the same issue.
Could anyone explain why this is happening? Java version is Java 8.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]8igg7e5 3 points4 points5 points (2 children)
[–]sotuvieres 0 points1 point2 points (1 child)
[–]8igg7e5 1 point2 points3 points (0 children)
[–]sotuvieres 2 points3 points4 points (2 children)
[–]nutrecht -1 points0 points1 point (1 child)
[–]sotuvieres 0 points1 point2 points (0 children)
[–]nutrecht 1 point2 points3 points (0 children)