What does java.util.*;
do? Why do we include it at the beginning of our files?
-
2It's one of the many imports that come with the Java language. This question however is not appropriate for SO. https://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html – Brunaldo Oct 20 '16 at 17:53
-
java.util is a `package` label in java. You should learn about what a package is and why it is useful – ControlAltDel Oct 20 '16 at 17:53
-
You are missing the `import`: the full statement is `import java.util.*;`. It doesn't mean anything without that. – Andy Turner Oct 20 '16 at 17:53
-
Read this: https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html – Andy Turner Oct 20 '16 at 17:55
-
Why is it usefull? what can i do whit this statement? – tizio Oct 20 '16 at 17:58
-
@tizio Read a tutorial, or preferably several. – Kayaman Oct 20 '16 at 18:10
2 Answers
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.

- 387
- 5
- 11
-
Hello. If you are guessing "why the downvote" my bet would go on expression "includes ... package" which may suggest that code from that package will be added to our class. But such assumption is wrong, `import` simply tells compiler where to look for classes which waren't named with their `full.package.Name`, but with simplified `Name`. Like `import java.util.*` allows us to write `List> list` and compiler will now change it into `java.util.List> list` (since Java expects full names) and not into `java.awt.List`. – Pshemo Oct 20 '16 at 19:05
-
2Thanks for pointing that out @Pshemo. I updated the comment to word things properly. – BFunk Oct 20 '16 at 20:00
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...

- 80
- 8
-
So if i put " import java.util. " I can use other function, is it right? – tizio Oct 20 '16 at 18:07
-
-
This answer is very, very wrong and should be deleted. It is inaccurate in both intention and wording. – E-Riz Oct 20 '16 at 18:56
-
@AndrewLee What do you mean by "For example, if you want to get today's date, there's no really good way to do this without using `import java.util.Date` or `import java.util.*`"? It is perfectly fine if I use `java.util.Date date = new java.util.Date();`. I don't need any `import` if I am willing to write full class names. `import` simply allows us to not write location of each class (which is needed by Java) and letting compiler do it for us. So if we add `import java.util.Date` we can now safely use `Date date = new Date()` and compiler will change each `Date` into `java.util.Date` for us. – Pshemo Oct 20 '16 at 19:09