-
Abs() Function???
Is it a function or something in java that makes negativ numer to positiv numbers. Do I have to import something like the math thing or something....thanks in advance.
I'm using NetBeans from Sun...and I can't belive that there is no help file for these kind of questions in it....:(
-
use Math.abs()
Code:
System.out.println(Math.abs(-4));
-
Thanks that worked. But just an other question. If I import the Math "thingy" like this...
import java.math.*;
shouldn't I then be able to write
abs(MyInteger);
in stead of
math.abs(MyInteger);
or what did I not understand about the import keyword....???
-
You use import for packages so that you don't have to specify the entire path to the classes it contains. eg...
Code:
import java.awt.*;
//...
Frame fra = new Frame();
instead of...
Code:
//...
java.awt.Frame fra = new java.awt.Frame();
imports can also be used for individual classes, too. Like in the above example, if all you will ever reference from the java.awt package is the Frame class, then you can do...
Code:
import java.awt.Frame;
to make it easier.
But to answer your question, no, it wouldn't work that way. abs() is a static method of the Math class. So you need to state Math.abs(...), otherwise, how will it know where to look for the method?
:)
-
OK...thanks for answering my dumb question.:)
-
btw: you don't import the math "thingy" ie class ;)
Math class is part of a package called java.lang
which is imported behind the scenes for you
Code:
public class Test
{
public static void main(String args[])
{
double someVal = -10.0;
someVal = Math.abs(someVal);
System.out.print(someVal);
System.exit(0);
}
}
I also show here the use of class System that is also stored
in java.lang
bsw
-
OK...thanks, for that one too...:)