Stack Overflow Asked by venky on December 25, 2021
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.
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.
Answered by Hulk on December 25, 2021
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
}
}
Answered by dyy.alex on December 25, 2021
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).
Answered by Pitto on December 25, 2021
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
Answered by Lê Việt Thắng on December 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP