• Post Reply Bookmark Topic Watch Topic
  • New Topic

abs() in Math class  RSS feed

 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(" value " + Math.abs(-23.3L));

compiler error..

pl explain what's the problem
 
Hung Yee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to tell the compiler that "-23.3L" is a Long integer type, but that's impossible because the number -23.3 is definitely not an integer.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler interprets literal floating point values as type double, and literal integral values as type int. The suffix L (for long) can be applied only to literal integral values -- not floating point.

So given Math.abs(-23.3L), the compiler will interpret the -23.3 as a double, then come to the "L" and say, "Hey, where's the closing parenthesis?"

If you really want -23.3 to be interpreted as an integral type (such as long), then you'll need an explicit cast, like Math.abs((long)-23.3).
[ January 27, 2005: Message edited by: marc weber ]
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should give 23.3 ...u shudnt have an 'L' when its a double
 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replies..
Appreciated
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!