• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Can you cast a method argument?  RSS feed

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method:
public static void methodName( long x , int y )
If I want to use this method but some of my y's are longs, can i do this in those cases:
methodName( sampleInteger , ( int )( sampleLong ) );
I got a compile error but I'm wondering if there is another way to do this. If not, do you have to make a new, slightly different method for these cases?
Any help would be much appreciated
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the below an example of what you are discussing? If so
it works fine.

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by David Junta:
If I want to use this method but some of my y's are longs, can i do this in those cases:
methodName( sampleInteger , ( int )( sampleLong ) );


Hi
IMHO if some of your "y's" are long, then make the parameter long. Then just cast the result to an int if and when you need it. This will guarantee you don't 'lose' information when converting a potentially long 'long' into an int.

Since an int is 32 bit and a long is 64 you could run the risk of losing information when you perform a 'narrowing cast' (64 bits pushed into 32 bits), or, casting your long variable to an int in the method call. But if you have a 32 bit (int) and cast it into a 64 bit (long) you NEVER will lose any information.
Hope this helps.

[This message has been edited by John Bateman (edited March 15, 2001).]
 
Whatever. Here's a tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!