// imports all classes of util package
import java.util.*;
// imports Scanner class of util package
import java.util.Scanner;
// what does this do?
import java.util.Scanner.*;
Is Scanner a package here?
Edit: Ok so import java.util.Scanner.* imports the public nested classes. But what if there was also a package called Scanner? What would the statement import java.util.Scanner.* do then?
import java.util.Scanner;
This imports Scanner (as you already know).
import java.util.Scanner.*;
This imports any public nested classes defined within Scanner.
This particular import statement is useless, as Scanner does not define any nested classes (and the import does not import Scanner itself). However, this can be used with something like import java.util.Map.*, in which case Entry (an interface nested in Map that is commonly used when dealing with maps) will be imported. I'm sure there are better examples, this is just the one that came to mind.
All of this is specified in JLS §7.5 (specifically, see §7.5.1: Single-Type-Import Declarations).
In response to the OP's edit:
Ok so import java.util.Scanner.* imports the public nested classes. But what if there was also a package called Scanner? What would the statement import java.util.Scanner.* do then?
In this case there would be a compilation error, since the package java.util.Scanner would collide with the type java.util.Scanner.
The asterisk after the classname imports public nested classes.
From the Java Tutorials:
Note: Another, less common form of import allows you to import the
public nested classes of an enclosing class. For example, if the
graphics.Rectangle class contained useful nested classes, such as
Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle
and its nested classes by using the following two statements.
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.
Related
Many times I'm writing my java code, import statements be like
import java.util.Scanner;
import java.util.ArrayList;
But when it is required to import more classes from java.util (or any other package), is there a way to import all the required classes in one line.
Like the code below
import java.util.Scanner,ArrayList; OR
import java.util.{Scanner,ArrayList};
Anyhow the above two lines didn't work. Is there a way to do so ?
Java language does not support that.
As far as importing top level classes is concerned, you have two options:
import package.path.ExactClass
or
import package.path.*
First one imports one and only one class, second one is wildcard import that imports all classes in that package.
import package.name.Class; // Import a single class,
// "its called Importing a Package Member"
import package.name.*; // Import the whole package,
// its called "Importing an Entire Package"
For your case,
import java.util.*;
Learn more at official doc
In Java, there are two valid forms of the import declaration:
import java.lang.Math;
import java.lang.Math.*;
In the latter, a wildcard is used. This form is known as a Type-Import-on-Demand declaration, but how is it different from the former? Does it also import the subpackages of java.lang.Math?
What if Math were a Type (e.g., a class)—would all of its inner classes be imported?
The documentation states:
Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements.
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.
So importing import java.lang.Math.*; will not import the Math class.
NOTE: You may also want to see Why is using a wild card with a Java import statement bad?
import java.lang.Math.*;
This will import all nested classes declared in the Math class in the java.lang package. References to nested classes could be given without the outer class (e.g., Foo for java.lang.Math.Foo).
import java.lang.Math;
This will import the Math class in the java.lang package. References to nested classes would have to be given with the outer class (e.g., Math.Foo).
Only immediately-nested types are imported. The declaration is not recursive.
This does work with types for importing inner classes.This also works with static import (for importing methods).
import static a.b.c.FooBar.*;
The statement
import java.util.ArrayList.*;
imports all nested classes of ArrayList, but not ArrayList itself. Since ArrayList does not have any (public) nested classes, the statement actually does nothing.
However, consider the interface Map, which defines the nested class Map.Entry. If we write
import java.util.Map.*;
at the start of the Java file, we can then write Entry<A,B> instead of Map.Entry<A,B> to refer to this nested class.
Importing members of classes usually makes the most sense if you are using static imports. Then you don't import nested classes, but static methods and variables. For example,
import static java.Math.*;
will import all static constants and methods from the Math class. Then you can use the static methods of the Math class by writing, e.g. sin(x) instead of Math.sin(x).
With the statement import java.util.ArrayList.*; you will import all nested classes declared into ArrayList class.
If you also want to import methods and const, for example, declares:
import static java.lang.Math.*;
Then you can use the constant PI in your code, instead of referencing it through Math.PI, and the method cos() instead of Math.cos(). So, for example, you can write:
double r = cos(PI * theta);
Basically Math is a final class, and it does not have further sub classes. There is no difference between import java.lang.Math.* and import java.lang.Math Both are one and the same. So I really dont see the need here to use the first kind of import.
My professor gave me the java.util.Arrays.sort static method and it shows an error that the import cannot be resolved. This does not happen to any of my other methods. Is this written right?
import java.util.Arrays.sort;
Edit: Does the .sort need to do something? I changed it to
import java.util.Arrays;
and there were no errors.
Importing in Java involves either importing a member class of a package, in this case (java.util is the package, Arrays is the class)
import java.util.Arrays;
or importing the entire package, which would be
import java.util.*;
There is no concept of importing an individual class method, which is what
import java.util.Arrays.sort;
was attempting to do , because sort is just one method of the java.util.Arrays class (there are many other methods of that class).
So if you attempt to do that, you will get the error message you did.
In Java, there are two valid forms of the import declaration:
import java.lang.Math;
import java.lang.Math.*;
In the latter, a wildcard is used. This form is known as a Type-Import-on-Demand declaration, but how is it different from the former? Does it also import the subpackages of java.lang.Math?
What if Math were a Type (e.g., a class)—would all of its inner classes be imported?
The documentation states:
Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements.
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.
So importing import java.lang.Math.*; will not import the Math class.
NOTE: You may also want to see Why is using a wild card with a Java import statement bad?
import java.lang.Math.*;
This will import all nested classes declared in the Math class in the java.lang package. References to nested classes could be given without the outer class (e.g., Foo for java.lang.Math.Foo).
import java.lang.Math;
This will import the Math class in the java.lang package. References to nested classes would have to be given with the outer class (e.g., Math.Foo).
Only immediately-nested types are imported. The declaration is not recursive.
This does work with types for importing inner classes.This also works with static import (for importing methods).
import static a.b.c.FooBar.*;
The statement
import java.util.ArrayList.*;
imports all nested classes of ArrayList, but not ArrayList itself. Since ArrayList does not have any (public) nested classes, the statement actually does nothing.
However, consider the interface Map, which defines the nested class Map.Entry. If we write
import java.util.Map.*;
at the start of the Java file, we can then write Entry<A,B> instead of Map.Entry<A,B> to refer to this nested class.
Importing members of classes usually makes the most sense if you are using static imports. Then you don't import nested classes, but static methods and variables. For example,
import static java.Math.*;
will import all static constants and methods from the Math class. Then you can use the static methods of the Math class by writing, e.g. sin(x) instead of Math.sin(x).
With the statement import java.util.ArrayList.*; you will import all nested classes declared into ArrayList class.
If you also want to import methods and const, for example, declares:
import static java.lang.Math.*;
Then you can use the constant PI in your code, instead of referencing it through Math.PI, and the method cos() instead of Math.cos(). So, for example, you can write:
double r = cos(PI * theta);
Basically Math is a final class, and it does not have further sub classes. There is no difference between import java.lang.Math.* and import java.lang.Math Both are one and the same. So I really dont see the need here to use the first kind of import.
In Java, there are two valid forms of the import declaration:
import java.lang.Math;
import java.lang.Math.*;
In the latter, a wildcard is used. This form is known as a Type-Import-on-Demand declaration, but how is it different from the former? Does it also import the subpackages of java.lang.Math?
What if Math were a Type (e.g., a class)—would all of its inner classes be imported?
The documentation states:
Note: Another, less common form of import allows you to import the public nested classes of an enclosing class. For example, if the graphics.Rectangle class contained useful nested classes, such as Rectangle.DoubleWide and Rectangle.Square, you could import Rectangle and its nested classes by using the following two statements.
import graphics.Rectangle;
import graphics.Rectangle.*;
Be aware that the second import statement will not import Rectangle.
So importing import java.lang.Math.*; will not import the Math class.
NOTE: You may also want to see Why is using a wild card with a Java import statement bad?
import java.lang.Math.*;
This will import all nested classes declared in the Math class in the java.lang package. References to nested classes could be given without the outer class (e.g., Foo for java.lang.Math.Foo).
import java.lang.Math;
This will import the Math class in the java.lang package. References to nested classes would have to be given with the outer class (e.g., Math.Foo).
Only immediately-nested types are imported. The declaration is not recursive.
This does work with types for importing inner classes.This also works with static import (for importing methods).
import static a.b.c.FooBar.*;
The statement
import java.util.ArrayList.*;
imports all nested classes of ArrayList, but not ArrayList itself. Since ArrayList does not have any (public) nested classes, the statement actually does nothing.
However, consider the interface Map, which defines the nested class Map.Entry. If we write
import java.util.Map.*;
at the start of the Java file, we can then write Entry<A,B> instead of Map.Entry<A,B> to refer to this nested class.
Importing members of classes usually makes the most sense if you are using static imports. Then you don't import nested classes, but static methods and variables. For example,
import static java.Math.*;
will import all static constants and methods from the Math class. Then you can use the static methods of the Math class by writing, e.g. sin(x) instead of Math.sin(x).
With the statement import java.util.ArrayList.*; you will import all nested classes declared into ArrayList class.
If you also want to import methods and const, for example, declares:
import static java.lang.Math.*;
Then you can use the constant PI in your code, instead of referencing it through Math.PI, and the method cos() instead of Math.cos(). So, for example, you can write:
double r = cos(PI * theta);
Basically Math is a final class, and it does not have further sub classes. There is no difference between import java.lang.Math.* and import java.lang.Math Both are one and the same. So I really dont see the need here to use the first kind of import.