6

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?

2
  • 1
    import java.lang.Math is redundant. You don't have to import things in java.lang .
    – khelwood
    Aug 4, 2017 at 14:30
  • thanks for your answer, i am aware of that we don't have to import java.lang but my question is what's the difference between two import statements Aug 4, 2017 at 14:33

6 Answers 6

6

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?

3

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).

2
  • are there more classes within Math class ? Aug 4, 2017 at 14:32
  • 1
    @bharatbhushan Nope. It's a pointless statement, but it is syntactically valid, and could be used in other circumstances. Stylistically, I recommend avoiding wildcard imports all together. A modern IDE will have an option for converting wildcard imports to individual imports automatically, which is quite useful. Aug 4, 2017 at 14:34
2

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.*;
2

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).

4
  • The matter is not only about nested classes, but also const and methods. Jan 15, 2015 at 9:29
  • @GiulioBiagini Only if you write import static. With only import, only classes are imported.
    – Hoopje
    Jan 15, 2015 at 10:09
  • But okay, I extended my answer although the OP didn't ask about static imports.
    – Hoopje
    Jan 15, 2015 at 10:18
  • Yes, but you will seldom need to import nested classes. For clarity it, is good to also specify the behavior that occurs in presence of the key static, that allows you to use const and methods without writing the class name before them. For sure, it will be more useful for the user to know this behavior rather than the other. Jan 15, 2015 at 10:26
1

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);
0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.