• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

assignments and casting

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Could anybody please guide me in the direction to a good site explaining casting and assignments.

For example:

Why is this legal:
short x = 7;

But....

if we have a method

testMethod(short param){}

and we call the method as follows:
testMethod(7);

This call to the method is illegal because we first of all have to cast the literal 7 to a short. Yet we can assign the literal 7 to a short variable.

I don't fully understand this.

Many Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your question involves two different types of conversions, assignment conversion and method invocation conversion.

Assignment conversion includes what are called narrowing primitive conversions. Basically if the type of the variable on the left is a byte, char, or short, and the type on the right of the assigment statement is a compile-time constant of type byte, char, short, or int that is within the range of the variable on the left, then a narrowing primitive conversion will be performed.

Method invocation conversion does not contain the narrowing primitive conversions.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assignment Conversion vs. Method Invocation Conversion
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Acetylsalicylic acid is aspirin. This could be handy too:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic