aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit Narrowing and Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit Narrowing and Casting" Watch "Implicit Narrowing and Casting" New topic
Author

Implicit Narrowing and Casting

Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108

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


---<br />SCJP 1.4
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 07, 2004
Posts: 108
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
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
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

Joined: Jul 02, 2002
Posts: 1865
The compile-time constant could be a literal or a variable that has been declared "final".
Murtuza Akhtari
Ranch Hand

Joined: Aug 07, 2004
Posts: 108
Thanks Dan,

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

Thanks once again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicit Narrowing and Casting
 
Similar Threads
Doubt on Dan's Question
Doubts in Dan's Exam:
Implict Cast Narrowing Doubts
Dan's Operator C Exam
Question about Dan's Mock (Dec. 4)