Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abs()

 
Mike Cunningham
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,
Short arguments in a method invokation are converted to
int. See JLS 5.3
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Sreenivasa Majji
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Hall
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic