3

I'm new to java and I'm trying to figure out how the Math functions work. I can't figure out what I'm missing.

Here's the entire program:

    public class Math {

    public static void main(String args[])
      {
        double x = Math.abs(4); 
        System.out.println(x);   
      }
    }

When I try to compile it, jGRASP says, "Math.java:5: error: cannot find symbol double x = Math.abs(4);"

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
rayden54
  • 251
  • 1
  • 5
  • 14

2 Answers2

9

You called your class Math, so the built-in java.lang.Math class can't be resolved. So Java thinks you're attempting to call your own abs method that doesn't exist.

Call your class something else, or refer to Math.abs with a fully qualified class name: java.lang.Math.abs(4).

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • I tried changing the class name (and file name) to MyTest. I've still got the same error. Although java.lang.Math.abs(4) does work. – rayden54 Oct 03 '13 at 22:54
  • 3
    Delete Math.java and Math.class from your directory; your `Math` class is still being picked up. – rgettman Oct 03 '13 at 22:56
0

You could also try:

public class MyTest {

public static void main(String args[])
  {
    double x = java.lang.Math.abs(4); 
    System.out.println(x);   
  }
}
Skylion
  • 2,696
  • 26
  • 50