2
\$\begingroup\$

New to code golf and tried finding an answer to this question but couldn't (surprisingly)

When scoring my golfed Java code I was told that I need to add in my imports as part of my score. My question is, if for example I am using a List. Do I need to use import java.util.* or (if it is only appearing once in my code anyway) use java.util.List<T> list = whatever; when I make the actual call which would obviously be shorter.

\$\endgroup\$
  • 3
    \$\begingroup\$ You do not need import if you do java.util.List. \$\endgroup\$ – Leaky Nun Aug 16 '16 at 13:36
  • \$\begingroup\$ Related (but not quite a dupe) \$\endgroup\$ – DJMcMayhem Aug 16 '16 at 14:00
3
\$\begingroup\$

You don't need to use an import if you fully qualify the class, as you guessed and Leaky Nun said in comments. Usually it's shorter this way, unless you need to use a class multiple times.

This is included in Tips for golfing in Java, by the way, which is a recommended read if you haven't yet.

\$\endgroup\$
2
\$\begingroup\$

I don't know very much about java, but if

java.util.List<T> list = whatever

Is valid without the import, the you do not need to use

import java.util.*

In general, I would say only you need whatever it takes to make your program successfully compile/run. If you are using a function instead of a full program, you must include any necessary includes/imports so that you could copy and paste the entire submission into a full program, and it would successfully compile/run.

However, if there is another approach that allows you to avoid the wordy imports, by all means, use this shorter approach! As long as it is still valid java, remove any imports you want.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .