Как импортировать math в java
Перейти к содержимому

Как импортировать math в java

  • автор:

Using Package Members

The types that comprise a package are known as the package members.

To use a public package member from outside its package, you must do one of the following:

  • Refer to the member by its fully qualified name
  • Import the package member
  • Import the member's entire package

Each is appropriate for different situations, as explained in the sections that follow.

Referring to a Package Member by Its Qualified Name

So far, most of the examples in this tutorial have referred to types by their simple names, such as Rectangle and StackOfInts . You can use a package member's simple name if the code you are writing is in the same package as that member or if that member has been imported.

However, if you are trying to use a member from a different package and that package has not been imported, you must use the member's fully qualified name, which includes the package name. Here is the fully qualified name for the Rectangle class declared in the graphics package in the previous example.

You could use this qualified name to create an instance of graphics.Rectangle :

Qualified names are all right for infrequent use. When a name is used repetitively, however, typing the name repeatedly becomes tedious and the code becomes difficult to read. As an alternative, you can import the member or its package and then use its simple name.

Importing a Package Member

To import a specific member into the current file, put an import statement at the beginning of the file before any type definitions but after the package statement, if there is one. Here's how you would import the Rectangle class from the graphics package created in the previous section.

Now you can refer to the Rectangle class by its simple name.

This approach works well if you use just a few members from the graphics package. But if you use many types from a package, you should import the entire package.

Importing an Entire Package

To import all the types contained in a particular package, use the import statement with the asterisk (*) wildcard character.

Now you can refer to any class or interface in the graphics package by its simple name.

The asterisk in the import statement can be used only to specify all the classes within a package, as shown here. It cannot be used to match a subset of the classes in a package. For example, the following does not match all the classes in the graphics package that begin with A .

Instead, it generates a compiler error. With the import statement, you generally import only a single package member or an entire package.

Be aware that the second import statement will not import Rectangle .

Another less common form of import , the static import statement, will be discussed at the end of this section.

For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).

Apparent Hierarchies of Packages

At first, packages appear to be hierarchical, but they are not. For example, the Java API includes a java.awt package, a java.awt.color package, a java.awt.font package, and many others that begin with java.awt . However, the java.awt.color package, the java.awt.font package, and other java.awt.xxxx packages are not included in the java.awt package. The prefix java.awt (the Java Abstract Window Toolkit) is used for a number of related packages to make the relationship evident, but not to show inclusion.

Importing java.awt.* imports all of the types in the java.awt package, but it does not import java.awt.color , java.awt.font , or any other java.awt.xxxx packages. If you plan to use the classes and other types in java.awt.color as well as those in java.awt , you must import both packages with all their files:

Name Ambiguities

If a member in one package shares its name with a member in another package and both packages are imported, you must refer to each member by its qualified name. For example, the graphics package defined a class named Rectangle . The java.awt package also contains a Rectangle class. If both graphics and java.awt have been imported, the following is ambiguous.

In such a situation, you have to use the member's fully qualified name to indicate exactly which Rectangle class you want. For example,

The Static Import Statement

There are situations where you need frequent access to static final fields (constants) and static methods from one or two classes. Prefixing the name of these classes over and over can result in cluttered code. The static import statement gives you a way to import the constants and static methods that you want to use so that you do not need to prefix the name of their class.

The java.lang.Math class defines the PI constant and many static methods, including methods for calculating sines, cosines, tangents, square roots, maxima, minima, exponents, and many more. For example,

Ordinarily, to use these objects from another class, you prefix the class name, as follows.

You can use the static import statement to import the static members of java.lang.Math so that you don't need to prefix the class name, Math . The static members of Math can be imported either individually:

Once they have been imported, the static members can be used without qualification. For example, the previous code snippet would become:

Obviously, you can write your own classes that contain constants and static methods that you use frequently, and then use the static import statement. For example,

How to Import Math in Java?

This tutorial will demonstrate how to import math in Java.

How to Import Math in Java?

As we discussed above, the Math class belongs to java.lang package, so it helps to access the Math class implicitly. Now we will look at how we can import the Java Math class.

Syntax
The syntax to import any package or class is given as:

To import the package “java.lang” with the “Math” class, we will write something like this:

However, the methods and variables of the Math class are static, so you can access them by using two ways:

  • Import Java Math class without using import statement
  • Import Java Math class by using import statement

We will now check each of the mentioned methods one by one!

Method 1: Import Java Math Class Without Using Import Statement

In Java, you can use the “Math” class directly without importing it because the Math class belongs to the “java.lang” package that allows using the associated classes implicitly. In this way, we can access the methods of the Math class with the class name. For example:

Have a look at the given examples for calling the static methods and variables of the Math class without importing them.

Example 1
In this example, we will access the “PI” static variable of the “Math” class to get the value of Pi. The most common example of using Pi is finding the area of a circle in any language. In the Java programming language, you can call the PI variable without importing “java.lang.Math package” as follows:

Output

Example 2
In the below example, we will call the “max()” method of the Math class to find the maximum number between “40” and “87”:

Output

Example 3
Here, we will find out the square root of “100” using the “Math.sqrt()” method:

Output

Method 2: Import Java Math Class Using Import Statement

You can also utilize the “import” statement to import the Math class as follows:

Asterisk (*) means “all”, and it signifies that all of the static variables and methods of the Math class will be imported.

The other way to use the import statement is add the variable or method name at the end like this:

The above-given statement will only import the “PI” static variable from the Math class.

Note: This method is more useful as it only requires a one-time definition at the top of the program. After that, you can utilize any method or variable without importing the Math class again and again.

Example 1
Here, we will first import the “java.lang.Math.*” class. Next, we will calculate the square root of a number “100” using the “sqrt()” method:

Then, calculate the maximum number between “40” and “87” with the help of the “max()” method of Math class:

Note that we are accessing both methods without mentioning the Math class, as it is imported before:

Output

Example 2
Now, we will import the PI variable with the Java Math class:

Then, we will print out its predefined value using the “System.out.println()” method:

Output

Example 3
In this example, we will calculate the power of “10” using the “pow” method of the Math class:

Output

We have gathered all the basic and important information related to importing Math in Java.

Conclusion

To import Math in Java, you can use the “java.lang” package to access the methods or variables of the Java Math class with their class name. Another way to import a Math class is to add the “java.lang.Math.*” import statement at the top of the code. It will allow access to all the members and the variables of the Math class without mentioning the class name. In this tutorial, we discussed different ways to import Math in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

How to static import math class in Java

Here we will see how can we import math class in Java.

As we already know to import any class from any package we use the following notation

Here java is package util is subpackage and Scanner is a class.

By importing the Scanner class we can use all its method in our program.

In Java java.lang is the default package so we don’t require to import this package when using classes of this package like System, Object, String classes.

If you will see java.lang package you will find Math is also available in same package.

So it means we don’t have to import Math class to use in our program.

How to import math class in Java example

Math is an important class in Java that holds a lot of important methods and constants. For example, we can use this class to do logarithm, square root, and trigonometric calculations using its built in methods.

It is a public final class:

It belongs to java.lang package.

All methods and variables of this class are static. So, we can easily import these without using any import statements.

In this post, I will show you two different ways to import Math class in a Java program.

Method 1: Import without using any import statement:

We can use any method or constants defined in the Math class by using the class name. We don’t have to import it because java.lang package is the default package in a Java program.

This class uses different constants and methods of the Math class without using import since all of these methods and constants are static.

If you run this program, it will print the below output:

Method 2: Import using static import:

We can also use static import to import the members of the Math class. We can import all methods and constants or we can import only specific members.

If you use static import, we don’t have to use Math class to access.

Import specific members:

For example, the below program uses static import to import only PI and E.

import static statements are used for static import. If you run this program, it will print the values of PI and E as defined in java.lang.Math class.

Note that we don’t have to use the classname if we use static import.

Static import all members:

Instead of importing specific members, we can also use * to import everything defined in a class.

We can use anything defined in the Math class if we use * to import all.

How to use static import with overloading methods:

Overloading methods can be used in a similar way. We can use these methods directly and based on the parameters, it will pick the specific method.

Here, the first method calls:

and the second method calls:

We don’t have to define specifically, it will automatically decide which method to call based on the parameters.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *