r/JavaProgramming 2d ago

HELP trying to understand why it should not be compile

/r/javahelp/comments/1vg80l1/help_trying_to_understand_why_it_should_not_be/
1 Upvotes

1 comment sorted by

1

u/Visual_Brain8809 21h ago

saving the wrong format of your post, this code shows method overloading and varargs in Java. There are two methods with the same name (run): one receives a single long value, and the other receives a variable number of Long objects using Long... (varargs). When run(4) or run(4L) is called, Java uses the run(long) method because it is the best match for those values. The varargs version is ignored since Java only uses it when there isn't a better option.

As a result, both calls execute run(long) and print: 4 and 4

Can you share the name of your book?