Why won't abs() in java work? - java

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.

Related

Java Normal Import [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.

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.

import static method in xtned (generated java)

It is possible to import just one static method, or member in extend? I know that it is possible to import all static methods by *, like in this example ...
import static example.Types.*
But I need only one.
Yes it is possible.
import static System.out;
You can then merely write
out.println("Doobedoo");
Which will save you from typing the System.out part several times over but is potntially confusing for anyone trying to follow you code.
Use it sparingly and generally only for constants ( static final ).
If what you're looking for is something like the using keyword from C# or C++ java doesn't have that feature.
Java 5 added an import static option that allows static variables (typically constants) to be referenced without qualifying them with a class name.
For example, after
import static java.awt.Color;
It would then be possible to write
Color background = RED;
instead of
Color background = Color.RED;
Example above have been taken from: Java: packages and Import.
The site used for the example seems to be out of date, sorry any trouble this can cause
See How java import works for more detail.

What is static imports and what is its importance in java

While reading some java books, I came to know about static imports.
I have some doubts in my mind.
What is static imports.
When and why to use it.
Explaination with examples will be helpful.
One example is JUnit tests
import static org.junit.Assert.assertEquals;
...
assertEquals(x, y);
Imports are typing shortcuts. A "regular" import is a shortcut down to the class level...
import java.util.List
Let's you just use
List l;
Instead of
java.util.List l;
A static import is a shortcut down to the method level. The method must be static, since there is no instance to associate with it...
import static java.lang.Math.abs
Lets you just use
x = abs(y);
instead of
x = java.lang.Math.abs(y);
Imports do not effect your compiled output or running code in any way. Once something is compiled there's no way to tell if the original source had imports or not.

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