• 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

Implicit Narrowing and Casting

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

This is Q20 from Dan's Mock Exam 3
The answer is given as c.
Explanation: Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa.

but then how can byte,short,char and int be implicitly casted as shown by the explanation of the following questions also on Dan's exams



The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char.





This was in Dan's Mock exam 1 Q22



If both operands of a binary arithmetic expression are of type byte, char or short; then both are implicitly widened to type int, and the result of the expression is of type int.



This is from Dan's Mock exam 2 Q11


Thanks
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I'm not quite sure what you're asking here, so please forgive me if my answer seems to miss the mark completely!

The second bit you quoted (about binary arithmetic expressions) is irrelevant - there isn't a binary arithmetic expression in sight. The first bit explains things nicely.The only other conversion which could give problems is the first one, a narrowing conversion which can be made implicitly because the compiler knows that the number, although it is an integer, does fit in a byte. This obviously only works if the value you assign is a compile-time constant.

- Peter

PS. With "loss of precision" I mean that the precise value of the number may be lost. A double cannot faithfully represent all different values of a long (both are 64 bits wide, but the double needs some of those bits for the exponent).
[ August 14, 2004: Message edited by: Peter den Haan ]
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Peter,

Thanks for your reply...I m sorry if my question seemed obscure. (Now that i read it again...It didnt quite make sense to me either..

So let me re-phrase..


The compiler will implicitly do a narrowing conversion for an assignment statement if the right hand operand is a compile time constant of type byte, short, char, or int and the value falls within the range of the variable on the left and if the variable is of type byte, short, or char.



and then this quote here says

Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa



arent they contradictory???...coz the first one says it can be implicitly casted if the left operand is of type byte char or short and the value falls within the range of the left operand...whereas the second one suggests that you need an explicit cast for short and chars due to sign differences
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Murtuza,

You missed a key requirement for an implicit narrowing conversion in an assignment expression, the right hand operand must be a compile-time constant. When the right hand operand is a compile-time constant, then the compiler can determine if it can be safely assigned to the variable that is the left hand operand.

If the right hand operand is not a compile-time constant, then the compiler has no way of knowing what that value might be at run-time.
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dan Chisholm:
Murtuza,

You missed a key requirement for an implicit narrowing conversion in an assignment expression, the right hand operand must be a compile-time constant. When the right hand operand is a compile-time constant, then the compiler can determine if it can be safely assigned to the variable that is the left hand operand.

If the right hand operand is not a compile-time constant, then the compiler has no way of knowing what that value might be at run-time.



Hi Dan,
But isnt 's' a compile time constant since 'b' is a compile time constant??

or is it that for it to be a compile time constant it has to have an assignment to a numeric value(no variables) or a final variable which has been declared and assigned in the same line !!
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compile-time constant could be a literal or a variable that has been declared "final".
 
Murtuza Akhtari
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan,

The questions on your mock exams really makes a person think !!!

Thanks once again
 
No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic