import static java.lang.Math.* 和 import static java.lang.Math.sqrt 之间有什么区别?

huangapple go评论63阅读模式
英文:

What is the difference between import static java.lang.Math.* and import static java.lang.Math.sqrt

问题

我有点迫不及待地想知道import static java.lang.Math.*和import static java.lang.Math.sqrt之间的区别。

我知道,Math.*允许我们访问Math类的所有静态方法。而Math.sqrt只提供对一个方法sqrt的访问。

这是唯一的区别吗?如果我在我的代码中只使用sqrt(),最佳实践是什么?我应该使用

import static java.lang.Math.sqrt;
(还是)
import java.lang.Math;

依我看,我会选择使用import static java.lang.Math.sqrt;,因为我只在我的代码中使用sqrt()。
看起来这是一个愚蠢的问题。但是,需要知道最佳实践。所以,在这里发布。

英文:

I'm bit eager to know the difference between import static java.lang.Math.* and import static java.lang.Math.sqrt.

I know, Math.* allow us to access all the static methods from Math class. Whereas Math.sqrt provides an access to one and only method sqrt.

Is this the only difference? If I use only sqrt() in my code, what is the best practice to use. Should I use

import static java.lang.Math.sqrt;
(OR)
import java.lang.Math;

As per me, I would go for import static java.lang.Math.sqrt; as I use only sqrt() in my code.
Seems it's silly question. But, need to know good practice. So, posting here.

答案1

得分: 2

你不需要专门导入java.lang.Math,或者java.lang的任何部分,而且你的代码片段可能包含一些复制/粘贴错误,正如khelwood的评论中所解释的那样。

如果我们想从一个更通用的角度来考虑这个问题,如果你导入整个类,会使用更多的内存,如果你没有使用所有导入的方法,导入所有这些方法就没有意义,这只会是资源的浪费。

使用像IntelliJ这样的高级IDE,你可以启用自动导入和代码分析,这些最佳实践将会被自动建议并在你的代码中以警告和错误的形式直接由IDE提供,从而加强执行。

我的建议是采用类似的解决方案,因为这会立即加快和改进你的编码。

或者,如果你不喜欢自动导入的想法,你可以使用优化导入功能,并通过简单的快捷键(control + alt + o)获得类似的结果。

英文:

You don't specifically need to import java.lang.Math, or any part of java.lang, and your snippet maybe contains some copy / paste errors, as explained in the comment by khelwood.

If we want to think about a more generic question on the topic if you are importing the whole class you are using more memory and if you are not using all the imported methods it doesn't make sense to import it all, it would be just a waste of resources.

Using an advanced IDE like IntelliJ you can enable automatic import and code analysis and this kind of best practises will be automatically suggested and enforced in your code with warnings and errors provided directly from the IDE.
My suggestion is adopting a similar solution because it will speed up and improve your coding right away.

Or, if you don't like the idea of automatic import, you can use the Optimize Import function and obtain a similar result with a simple shortcut (control + alt + o).

答案2

得分: 1

一般引入类的方式:import java.lang.Math.*;

静态引入类的方式:import static java.lang.Math.*;

两者的区别在于:

通常引入类需要使用ClassName.method();来调用类中的静态方法;

public class Test {
     public static void main(String[] args) {
         System.out.println(Math.sqrt(4)); //需要添加类名前缀
     }
}

静态引入后,直接使用method();来使用静态方法。

import static java.lang.Math.*;
public class Test {
     public static void main(String[] args) {
         System.out.println(sqrt(4)); //直接调用方法
     }
}
英文:

The general way to introduce classes: import java.lang.Math.*;

The way to introduce classes statically: import static java.lang.Math.*;

The difference is that:

Generally, the introduction requires the use of ClassName.method(); to call the static method in the class;

public class Test {
     public static void main(String[] args) {
         System.out.println(Math.sqrt(4)); //Need to add the class name prefix
     }
}

After static introduction, use method(); directly to use static method.

import static java.lang.Math.*;
public class Test {
     public static void main(String[] args) {
         System.out.println(sqrt(4)); //Call the method directly
     }
}

答案3

得分: 0

如果您只使用sqrt函数,您应该导入静态包java.lang.Math.sqrt;
如果您使用java Math的其他函数,您应该导入全部:java.lang.Math。
但是导入java.lang.Math.* 会导致更多的内存开销。

英文:

if you use only function sqrt, you should import package static java.lang.Math.sqrt;
if you use other function of java Math, you should import all : java.lang.Math.
but import java.lang.Math.* leading to more memory overhead

答案4

得分: 0

类被整体导入,不需要提到方法名或星号。

import java.util.ArrayList;
import java.util.List;

来自包java.lang的类无需导入:

package world;
// 不需要导入任何内容

public class Main {

    public static void main(String[] args) {	
        double a = 3;
        double b = 4;
        double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
        System.out.println(c);
    }    
}

但是,如果您需要使用该类的许多方法,重复类名可能会有点繁琐。因此,Java 提供了一个名为“静态导入”的特性:

使用此特性,您可以选择要省略类名的特定方法或常量。

import static java.lang.Math.pow;

public class Main {

    public static void main(String[] args) {	
        double a = 3;
        double b = 4;
        double c = Math.sqrt(pow(a, 2) + pow(b, 2)); // 显式 Math.sqrt,隐式 Math.pow
        System.out.println(c);
    }

}

或者导入类的所有方法:

import static java.lang.Math.*;

public class Main {

    public static void main(String[] args) {	
        double a = 3;
        double b = 4;
        double c = sqrt(pow(a, 2) + pow(b, 2));
        System.out.println(c);
    }

}

来自官方 Oracle 教程的重要注意事项:

那么什么时候应该使用静态导入?非常有限地使用! 仅在您本来会想要声明常量的本地副本,或者滥用继承(常量接口反模式)时使用它。

因此,回答您的具体问题:

只有在您想要在使用时省略类名时才使用

import static java.lang.Math.sqrt;

在所有其他情况下,只需省略导入语句。

英文:

Classes are imported as a whole, there is no need to mention a method name or an asterisk.

import java.util.ArrayList;
import java.util.List;

Classes from package java.lang don't need to be imported:

package world;
// no imports needed

public class Main {

	public static void main(String[] args) {	
		double a = 3;
		double b = 4;
		double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
		System.out.println(c);
	}    
}

However, if you need to use lots of methods from that class, it can get a bit tedious to repeat the class name. Therefore, java provides a feature named static import

Using this, you can choose specific methods or constants for which you want to omit the class name

import static java.lang.Math.pow;

public class Main {

	public static void main(String[] args) {	
		double a = 3;
		double b = 4;
		double c = Math.sqrt(pow(a, 2) + pow(b, 2)); //explicit Math.sqrt, implicit Math.pow
		System.out.println(c);
	}

}

or all methods of a class

import static java.lang.Math.*;

public class Main {

	public static void main(String[] args) {	
		double a = 3;
		double b = 4;
		double c = sqrt(pow(a, 2) + pow(b, 2));
		System.out.println(c);
	}

}

Important caution from the official Oracle tutorial:

> So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern).

So, answering your specific question:


Only use

import static java.lang.Math.sqrt;

if you want to be able to omit the class name when using it.

In all other cases, just omit the import statement.

huangapple
  • 本文由 发表于 2020年7月24日 16:02:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63069371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定