1

Are there any performance advantages or disadvantages to using either? I notice in eclipse whenever I need to import something due to a class missing, it imports only that specific class. Understandably because that is the only class I am using at the time. For instance, I could have package.models. This models folder contains 8 classes that define the structure of data tables. This may be a small example but are there any performance advantages/disadvantages between using package.models.* compared to package.models.specific_model_name?

I asked this because when I first started in my programming career I used to get slaughtered for using "select *" in my queries. I was curious if the same holds true for the importing of classes, which I have never really heard anyone mention before throughout my career.

Edit: Thank you for all who responded. I unfortunately do not have enough reputation to give anyone any upvotes. I also would more lean towards this one being a duplicate, Import package.* vs import package.SpecificType.

Community
  • 1
  • 1
user3241191
  • 505
  • 2
  • 5
  • 12
  • It may well be. Most of what I saw was about best practices but was looking more for performance advantages/disadvantages which seem to be none. – user3241191 Jan 28 '14 at 16:57

3 Answers3

2

There is no runtime performance impact at all.

Use which ever one suits you best. (Personally I prefer not to use wildcards as you tend to get lots of name collisions if you have multiple wildcard imports).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

While there isn't a runtime performance hit, it is bad practice to use *

Reason being as an outsider reading your code, I want to know EXACTLY which class you are using. There often are a number of classes that are implemented in different ways in different packages. Avoiding * makes it obvious which you are using.

For example, java.awt.List; vs java.util.List;

It is very possible you will get obscure naming collisions if you rely on *.

Having said that, all the * does is tell the java compiler where it should look to resolve class definitions. So saying * says, "hey compiler, when looking for classes feel free to look anywhere that matches the pattern blah.blah.*" So in theory, there is a VERY minimal performance hit in compilation when making the compiler search this pattern for the class. But, you would never notice it, and this hit is totally negligible.

75inchpianist
  • 4,112
  • 1
  • 21
  • 39
  • "it is bad practice.". That is what I was looking for. I've worked in various languages, even C# and I've always saw .*. I never thought about it until doing some android development within eclipse and it only importing what was needed. It would make sense to only import what you need for the reason you specified and for the fact that you actually learn and only use what you need. – user3241191 Jan 28 '14 at 16:53
  • "as an outsider reading your code, I want to know EXACTLY which class you are using." Exactly. And when I look at my own code a month later, *I* am the outsider. – aliteralmind Jan 28 '14 at 16:59
  • That is a very good point aliteralmind. That is why I think every coders signature line should say "remember to comment your code". It's so much easier for another person, or as you said even yourself, to remember why something was built and its use quicker than having to try and become a detective from the start due to lack of proper commenting. – user3241191 Jan 28 '14 at 17:10
1

Try to avoid using [package].* because if someone other than you were to read your code, they would want to know what class you are using from that package.

  • All these answers seem the best and I am agreeing with. Throughout my career, I have never had anyone mention this or remember reading about specifying only import what you need as a lot of the examples you learn from use .*. It's kind of like properly indenting your code, you don't have to but it just makes it messier for anyone else who looks at it. – user3241191 Jan 28 '14 at 16:55