Java Normal Import [duplicate] - java

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.

Related

The import (java.util.Arrays.sort) cannot be resolved

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.

Why won't abs() in java work?

I have always had a question about java.lang.Math: (It might by very basic)
Why do I have to Math.abs(-100) and can't to abs(-100)?
I figure that Math is a class. And abs is a static method. But why can I not simply import java.lang.Math and use abs(-100)?
You can import all the methods in Math:
import static java.lang.Math.*;
or just the one method you want:
import static java.lang.Math.abs;
Normal imports just import classes, making that class available via its short name.
abs is a static method and in order the compiler knows where it's defined, you have to specify the class (in your case - Math).
Note that you could do a static import on Math.abs and then you'd be able to just do abs(-100) instead of Math.abs(-100). In this case you'll have to add an import statement like this one:
import static java.lang.Math.abs;
Note also that in Java, unlike JavaScript and PHP, there aren't any public functions, which is why import statements are important.
java.lang.Math is statically imported in every Java Class.
static import java.lang.Math;
Every class of the java.lang package is imported that way.
As you know everything in java is within the class. So their can be only two alternatives.
Static Function and
Non Static Function
And java.lang.Math is a utility library. Creating object of this is not worth for you. so Java guys created all the functions static in this library.
And for your question you can call a member function directly if and only if they are member of same class.

What do we import with a wildcard after the class name? [duplicate]

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.

Difference between java.util.Scanner and java.util.Scanner.*

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

What does a Type-Import-on-Demand Declaration 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.

Categories

Resources