Reputation: 13
java.util.* import all the class under it, while java.util.Scanner only imports Scanner class. So, is there any difference between them in time of execution between them?
Upvotes: 1
Views: 1835
Reputation: 272760
Unlike the #include
thingy in C, import
ing stuff in Java does not actually copy stuff, so performance-wise, java.util.*
and java.util.Scanner
are practically the same.
However, importing everything from a package can cause name conflicts. Look at how many classes there are that are called Scanner
:
If you just so happens to import everything from both java.util
and sun.tools.java
. The compiler will not be able to infer which Scanner
you are referring to.
Upvotes: 2
Reputation: 91
java.util.* will import the entire utility classes whereas java.util.Scanner will import only Scanner class. Now, it is up to you as if you want to take input from user then import only java.util.Scanner class specifically.
It is always best practice to import specific class because importing entire package may confuse you. Eg java has 2 Date classes in two different packages 1)java.util.Date 2)java.sql.Date
When we import an entire package using '*' and if any error occurs then we don't understand because of which class this error has occurred that's why it is always best practice to import specific class according to the requirement.
Thank you. Rupesh R. Bharuka
Upvotes: 0