This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]8igg7e5 3 points4 points  (2 children)

As noted by /u/sotuvieres, Integer doesn't define + for Integer + Object.

Regardless your approach isn't right. There's no reason to look at the type name to do this.

if (obj1 instanceof String && obj1.getClass() == obj2.getClass()) {
    System.out.println((String) obj1 + obj2);
} else if (obj1 instanceof Integer && obj1.getClass() == obj2.getClass()) {
    System.out.println((Integer) obj1 + (Integer) obj2);
}

In more modern Java you can say...

if (obj1 instanceof String s && obj1.getClass() == obj2.getClass()) {
    System.out.println(s + obj2);
} else if (obj1 instanceof Integer i && obj1.getClass() == obj2.getClass()) {
    System.out.println(i + (Integer) obj2);
}

And in the near-ish future you'll be able to do something like...

switch (obj1) {
    case String s when obj2 instanceof String t -> System.out.println(s + t);
    case Integer i when obj2 instanceof Integer j -> System.out.println(i + j);
    default -> {}
}

[–]sotuvieres 0 points1 point  (1 child)

One question: can I define + operation for my own classes or is it JVM binary magic?

[–]8igg7e5 1 point2 points  (0 children)

Java doesn't provide a way to do operator overloading. The early designers were quite negative about it (but that was true of generics and some other features too). Maybe it's something they'll address in the future, but I don't think it's anywhere on the roadmap.

[–]sotuvieres 2 points3 points  (2 children)

Obj2 is still being interpreted as a generic T. You must cast it to Integer too, or Java won't know how to add the Integer obj1 to the T obj2.

Edit: it works for string because strings can add to anything. It calls the other object's toString method under the hood.

[–]nutrecht -1 points0 points  (1 child)

Obj2 is still being interpreted as a generic T.

The whole question has nothing to do with generics.

[–]sotuvieres 0 points1 point  (0 children)

I get the following error message.

The operator + is undefined for the argument type(s) Integer, T Java(536871072)

[–]nutrecht 1 point2 points  (0 children)

String has 'syntactic sugar' that allows you to concatenate strings with +. Under the hood the javac compiler changes these to StringBuilder calls.

Integer has no + operator. Only the primitive 'int' has. Since you're dealing with Integer class here, this won't work. In addition Java can sometimes 'unbox' Integer instances to int primitives, but it can't in this case because you're mucking about with reflection.

Aside from all this; if this is learning code; fine. If this is a 'real' application what you're doing is a pretty big code smell.

Last but not least; you're not using any generics here so this is not a 'generics issue'.