Should i use "*" instead of "Scanner" in "import java.util." | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Should i use "*" instead of "Scanner" in "import java.util."

what is the best and efficient way to use java.util & other syntax.

30th Aug 2017, 11:29 AM
Wahid Febrilianto
Wahid Febrilianto - avatar
5 Answers
+ 8
It really doesn't matter. It is not really imported, the import only shows the compiler where to search for the class. More like a reference.
30th Aug 2017, 11:36 AM
Tashi N
Tashi N - avatar
+ 4
No, there isn't any difference.
30th Aug 2017, 11:40 AM
Tashi N
Tashi N - avatar
+ 4
Using "Scanner" only imports the Scanner class from Java.util where "*" imports all classes in Java.util. Therefore, if you only want the Scanner class use Scanner as this prevents the compliler importing all of "util". However, if you want to import multiple classes from "util" use "*" to reduce boilerplate code.
30th Aug 2017, 12:17 PM
josh mizzi
josh mizzi - avatar
+ 3
If you are only using scanner and nothing else don't use *.(it imports all of the utilities) It might not make a difference if you have a small programm but if it's big it might. An analogy as an example: If you only need a hammer to do something, you won't carry all of your tools with you.
30th Aug 2017, 12:49 PM
Haris
Haris - avatar
0
will it make my code more efficient & better if i use the "*" although i just use one Scanner.
30th Aug 2017, 11:39 AM
Wahid Febrilianto
Wahid Febrilianto - avatar