This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

assignments and casting

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!