aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abs() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abs()" Watch "abs()" New topic
Author

abs()

Mike Cunningham
Ranch Hand

Joined: Nov 14, 2000
Posts: 129
The abs() method takes args as int, long, float or double. I passed it a value of type 'short' and it seemed to take it without a problem? Are there other value types that this can take...or was my value converted to an int?
- Mike
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Mike,
Short arguments in a method invokation are converted to
int. See JLS 5.3
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

Are there other value types that this can take...

umm....yes! char.


or was my value converted to an int?
yup! Remember enlarging conversions are done automatically.
However, you need a cast to do the shortening conversions.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Sreenivasa Majji
Ranch Hand

Joined: Jul 12, 2001
Posts: 224
Because abs() accepts double type variable/constant as an argument, you can pass any variable which can be casted into double. In this case, except byte, you can pass any primitive type data as an argument to abs().

Originally posted by Mike Cunningham:
The abs() method takes args as int, long, float or double. I passed it a value of type 'short' and it seemed to take it without a problem? Are there other value types that this can take...or was my value converted to an int?
- Mike


------------------
Sreenivasa Kumar Majji
Sun Certified Java Programmer
SCJP Mock Test


Sreenivasa Majji
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
Originally posted by sreenivasa majji:
Because abs() accepts double type variable/constant as an argument, you can pass any variable which can be casted into double. In this case, except byte, you can pass any primitive type data as an argument to abs().

Except byte, what's wrong with byte?
<code><pre>
public class Test
{
public static void main(String[] args)
{
byte b = -32;
System.out.println(Math.abs(b));
}
}
Output:
32
</pre></code>

[This message has been edited by Jim Hall (edited December 18, 2001).]
 
Don't get me started about those stupid light bulbs.
 
subject: abs()