This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# Question from mock exam

Punitha krishna
Greenhorn

Joined: Nov 22, 2000
Posts: 16
Which assignments are legal?
a. long test = 012;
b. float f = -412;
c. int other = (int)true;
d. double d = 0x12345678;
e. short s = 10;
I don't understand how b and d are correct.
Milind Deodhar
Ranch Hand

Joined: Dec 06, 2000
Posts: 32
b is correct because it is a int value and can be put in float without casting. (even if it is negative).
Second is correct, because I think double can hold the biggest value and no cast is required to put any primitive into double
(only cannot put boolean value)
Punitha krishna
Greenhorn

Joined: Nov 22, 2000
Posts: 16
Thanks It is clear now. I guess I didn't read the question properly.
David Roberts
Ranch Hand

Joined: Nov 03, 2000
Posts: 142
You probably already know this but:
float f = -412; //Would Pass (int fits in float)
float f = -412.0; //Would fail (double doesn't fit in float)
float f = -412.0f; //Would pass (explicitly stating float)
float f = (float)-412.0; //Would pass (Cast)
- David

David Roberts - SCJP2,MCP