• 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

the result of the assignment operator

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read in my book (K&B) that the result of the assignment operator is the right operand.

Then why does the following not compile?:

double d;
int i = ( d = 4 );

Since it should assign the literal 4 to i, and not the double d. But the compiler complains about possible loss of precision.

Thanks
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = ( d = 4 );

The first right operand is 4, an integer. This is widened to a double d. Now the result of this expression, the expression (d = 4), is a double.

When you try to assign this value, a double, to an int, it complains.

I believe every expression returns a value which can be utilized- is this true? Certainly every arithmetic expression. This could be investigated- does an assignment of objects return an object?
 
Jimmy Praet
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh yes, I guess it is because the right operand is being promoted to double then.

But what's the point then in stating that the right operand is returned, and not the left operand (after assigning it offcourse). Is there any special case where they will not be of the same type?

And yes, assigning object references also returns an object. e.g.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom is right
The thing here is that compiler doesn't bother about what value is stored in the variable. It just look at the type.
And as d is double it sees that you are trying to assigning a double value to interger variable and so yells about the loss of precision...

And this is also a reason that in inheritance some assignments of subclass-superclass are ok at compile time but fail at runtime as compliler is not able to know the value in the variables
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you give an example of failure at run time which works during compile time?
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tom Tolman:
Could you give an example of failure at run time which works during compile time?



myBase at some point in some far-away part of the program could have been assigned to a Sub object so the compiler has to allow this type of assignment, and if this were the case, the cast would work.
 
Oneal Shaha
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Louie that is nice example.
Thanks for contributing
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic