The statement java.util.*; imports all of the java.util package members so that you don't have to use a package member's fully qualified name. According to the JavaDocs here the package java.util
Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array)
Although this approach can seem more convenient and is sometimes appropriate you shouldn't always be including an import java.util.*; statement at the beginning of all of your files unless you are using a substantial amount of the members contained in the java.util package. Only include the members that you use like so:
import java.util.ArrayList;
import java.util.LinkedList;
By doing so it helps make you more familiar with each package member that you're using instead of blindly importing the whole package. The most important reason is that by using the wildcard character(*) you have a greater chance of coming across name ambiguities which can lead to errors.
import java.awt.*;
import java.util.*;
In the above code example, the class List becomes ambiguous because both packages have a List class.
Answer from BFunk on Stack OverflowI'm new to java, I've been seeing import java.util.* across the web on forums and other sites, I'm curious what does it mean? Is it anything like import java.util.Scanner or Random, etc?
I don't understand what does java.util. mean - Stack Overflow
What's the difference between import java.util.*; and import java.util.Date; ? - Stack Overflow
In Java, what does the import java.util.*; statement do?
Question 1Answer
a.
Imports all classes from the java.util package.
b.
Imports only the java package.
c.
Imports all classes from the java package.
d.
Imports nothing.
What is the difference between "import java.util.*" and "import java.io.*"?
The statement java.util.*; imports all of the java.util package members so that you don't have to use a package member's fully qualified name. According to the JavaDocs here the package java.util
Contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array)
Although this approach can seem more convenient and is sometimes appropriate you shouldn't always be including an import java.util.*; statement at the beginning of all of your files unless you are using a substantial amount of the members contained in the java.util package. Only include the members that you use like so:
import java.util.ArrayList;
import java.util.LinkedList;
By doing so it helps make you more familiar with each package member that you're using instead of blindly importing the whole package. The most important reason is that by using the wildcard character(*) you have a greater chance of coming across name ambiguities which can lead to errors.
import java.awt.*;
import java.util.*;
In the above code example, the class List becomes ambiguous because both packages have a List class.
Think of it like a library of methods that you now have access to. You are basically importing more functionality into your project
When you do
import java.util.*
you now have the ability to do things like create arrays, manipulate dates and so on... (https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html)
The .* means you are importing all of the util functions like java.util.Arrays or java.util.Date...
You probably have some other "Date" class imported somewhere (or you have a Date class in you package, which does not need to be imported). With "import java.util.*" you are using the "other" Date. In this case it's best to explicitly specify java.util.Date in the code.
Or better, try to avoid naming your classes "Date".
The toString() implementation of java.util.Date does not depend on the way the class is imported. It always returns a nice formatted date.
The toString() you see comes from another class.
Specific import have precedence over wildcard imports.
in this case
import other.Date
import java.util.*
new Date();
refers to other.Date and not java.util.Date.
The odd thing is that
import other.*
import java.util.*
Should give you a compiler error stating that the reference to Date is ambiguous because both other.Date and java.util.Date matches.