This question already has answers here:
Meaning of the import statement in a Java file
(5 answers)
Closed 6 years ago.
What does java.util.*; do? Why do we include it at the beginning of our files?
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...
Related
Does Java support grouping the imports like following:
import package.{Class1,Class2}
I know that * operator imports sub packages but I want to import specific ones.
In Rust or some modern languages it is supported like following:
use package::{Class1, Class2};
Are there any alternative instead of writing each import line by line specifically like this?
import package.Class1;
import package.Class2;
Java Language Specification, section 7.5. Import Declarations, shows:
An import declaration allows a named type or a static member to be referred to by a simple name (§6.2) that consists of a single identifier.
[...]
A single-type-import declaration (§7.5.1) imports a single named type, by mentioning its canonical name (§6.7).
A type-import-on-demand declaration (§7.5.2) imports all the accessible types of a named type or named package as needed, by mentioning the canonical name of a type or package.
A single-static-import declaration (§7.5.3) imports all accessible static members with a given name from a type, by giving its canonical name.
A static-import-on-demand declaration (§7.5.4) imports all accessible static members of a named type as needed, by mentioning the canonical name of a type.
As you can see, it's either a single named type, or all accessible types. No syntax for a list of types.
Side note: I almost never look at the import statements of my code. I let Eclipse manage that for me (Source > Organize Imports... (Ctrl+Shift+O)), so I don't really care about having many single-type import statements. The section with the imports is collapsed anyway, so I don't even have to scroll past them. Oh the joy of using a good IDE.
In java, only supported ways to import multiple classes are as follows :
A - import individual classes
import package.Class1;
import package.Class2;
B - import all classes in a package or subpackage
import package.*;
import package.subpackage.*;
Refer Oracle doc for more information : https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
No. Java doesn't have a construct to import a set of select classes using one statement. You either import all types from the package, or you import them one by one.
Using * lets you import all classes from the same package (not to import sub-packages, as you mentioned):
import package.*; //Both Class1 and Class2 are imported
import static package.Class1.* //All static members of Class1 are imported
The first form import package.* is usually discouraged because of the increased potential for name clashes (same class names from different packages). This is probably where import package.{Class1,Class2} would have helped, but there's no such construct in Java.
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?
Unlike the #include thingy in C, importing 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.
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
When you declare an import like this:
import com.microsoft.azure.storage.*;
Does that include everything in its subpackages too? For example, does it include this?
import com.microsoft.azure.storage.blob.*
If not, why not? (Edit: the "why" question is basically off topic. ignoring that bit when considering a correct answer.)
No it does not. It only imports everything in the package (i.e. the directory itself). Sub-directories are considered to be different packages, so you would need to:
import com.microsoft.azure.storage.*
import com.microsoft.azure.storage.blob.*
As to why the language designers chose to go this route, one can only guess, but the system that they decided to go with does allow for a more fine-grained approach.
yes you can import all the classes from an import but it does not make it possible to import multiple packages with similar names. For example import java.util*; does not also import java.util.prefs or .jar you have to import these all separately. I don't know if that answers your question, and to the why I am not really sure it just makes sense to do it this way. If you were to import similar packages that had the same static variables, but you only need two or three of the packages then you would get errors or code that does not run properly.
There's a name for these - type import on demand.
A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.
They're only also importing the package itself, and not any subpackages, as clarified by the example, emphasis mine:
import java.util.*;
causes the simple names of all public types declared in the package java.util to be available within the class and interface declarations of the compilation unit. Thus, the simple name Vector refers to the type Vector in the package java.util in all places in the compilation unit where that type declaration is not shadowed (§6.4.1) or obscured (§6.4.2).
does that include everything in / subdirectories too? including
something like this?
* stands for all the compilation units inside the package com.microsoft.azure.storage where sub packages are actually not compilation units and thus not fetched when you write myPack.*. Compilation unit includes class, interface, enum etc.
Is it possible to make a method in java that can import things? If so how is it done? If you are wondering why I am asking this, it is because I am making a library in java to make your code smaller.
This is not possible in Java. You have to import the resources (classes, interfaces, enums, static imports, etc.) in each file. At most, you can use less code by importing all the public classes and interfaces that belong to a package by using * wildcard. Example:
//Importing all classes and interfaces declared ad java.util package
import java.util.*;
I tired creating a package with one public class and one with multiple public classes and then import them through another package and both times the program worked. I understand the difference, in the first i can only use the methods of the one public class, but that class can use other classes in its package, and when more classes are public i can directly access all those classes through the alien package.
Now when we import java.util.Scanner, and we import java.util.ArrayList isn't this the same package? When we import our package with one public class it only shows the one public. That means that java.util has multiple public classes, or?
Also what does that java. mean, does it make the difference between my package and java library packages?
Yes, a package can have multiple public classes.
Creating a new package for every public class would be extremely cumbersome and provide no overview at all.
java. are just the standard libraries that come with the JDK/JRE.
Packages are really just like folders on your system. You can have multiple files in a folder without a problem.
Typically packages are structured in a way that they combine related classes to make it easier for the developer. If you want to have no packages at all, or you want a new package for every class: it doesn't matter. You'll be your own worst enemy, but it is surely possible.
import java.util.ArrayList;
means: let me use the class ArrayList, which is in the package java.util, by only typing ArrayList and not java.util.ArrayList.
java.util is the name of the package, nothing else. It's where the Java developers chose to store the ArrayList class. It's not different from any other package name, except you're not allowed to use the java package for your own classes, because it's reserved for standard Java classes.
import java.util.*;
means: let me use all the classes of the package java.util (ArrayList, LinkedList, Collection, etc.) by typing their simple name (ArrayList, LinkedList, Collection, etc.) instead of their fully qualified name (java.util.ArrayList, java.util.LinkedList, java.util.Collection, etc.)
All these classes are public (as documented in their javadoc). If they weren't public, you wouldn't be able to use them in your own classes.