Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abs()

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Short arguments in a method invokation are converted to
int. See JLS 5.3
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Something must be done about this. Let's start by reading this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic