aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicit Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicit Casting " Watch "Implicit Casting " New topic
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
Originally posted by Barry Gaunt:
Checkout Corey's Article On this Topic


Hehe - sometimes, Barry, I think you remember what I write better than I do. I had forgotten about that article.


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicit Casting