| Author |
Implicit Casting
|
prakash sodhani
Greenhorn
Joined: Nov 04, 2004
Posts: 11
|
|
Hi All, I am kind of confused with casting. Let say if we have float f=10; When i executed this it works fine. My question is...if 10 is interpreted as double (64 bits)...whats the reason above assignment passed. Same is the case with following: long w=10; float m=w; When is the implicit casting done??? Thanks Prakash
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
The 10 is not interpreted as a double. It's an int literal constant. The conversion is allowed because int -> long -> float is allowed. This is a widening conversion. The constant's value 10 is small enough to fit into a float without losing any information (precision). Even if precision is lost you will not get an error. [ November 07, 2004: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
|
Checkout Corey's Article On this Topic
|
 |
prakash sodhani
Greenhorn
Joined: Nov 04, 2004
Posts: 11
|
|
Thanks Barry. This article seems to be what I was looking for. -Prakash
|
 |
PNS Subramanian
Ranch Hand
Joined: Jul 13, 2004
Posts: 150
|
|
Barry, int -> long to ok. Is long -> float allowed ? Thought long was 8 bytes and float was 4 - could you please clarify.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
You too can write code: Output:
|
 |
Corey McGlone
Ranch Hand
Joined: Dec 20, 2001
Posts: 3271
|
|
Hehe - sometimes, Barry, I think you remember what I write better than I do. I had forgotten about that article.
|
SCJP Tipline, etc.
|
 |
 |
|
|
subject: Implicit Casting
|
|
|