This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Legal Data Types 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 "Legal Data Types" Watch "Legal Data Types" New topic
Author

Legal Data Types

Elaine Ware
Greenhorn

Joined: Mar 10, 2005
Posts: 6
Hi All -- I'm using the "Complete Java 2 Certification Study Guide", 3rd ed., by Phillip Heller and Simon Roberts, Sybex 2002. In the preparation exam for J2 Programmer, the first question is as follows:

-- In the following code fragment, what are the legal data types for the variable, answer? (Choose all that apply)

byte b = 1;
char c = 2;
short s = 3;
int i = 4;
float f= = 5f;
answer = b*c*s*i*f;

A. byte
B. char
C. short
D. int
E. float
F. double
G. long

--

The correct choices are E,F. Why is G not correct? Thanks for the advice.
[ March 10, 2005: Message edited by: Elaine Ware ]
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Possible loss of precision (see compiler error below), using long data type for answer.

byte b = 1;
char c = 2;
short s = 3;
int i = 4;
float f = 5f;
long answer = b*c*s*i*f;


[ March 10, 2005: Message edited by: Carol Enderlin ]
Ren� Star
Greenhorn

Joined: Feb 24, 2005
Posts: 13
For binary operands, the operators are widened to the largest type of the operators, and to integers if both operators are of a type smaller than int.

The order is:

byte -> short -
|-> int -> long -> float -> double
char -

The largest operator in b*c*s*i*f is of type float, so the result is also of type float.

Assignment to a smaller type needs a cast (with the exception of the byte/shot/char assignments with a fitting constant value).
Assignment to a wider type is automatic.

So the only valid types of the answer variable are types that are at least as wide as float. Long is smaller than float, so it is not valid.
Elaine Ware
Greenhorn

Joined: Mar 10, 2005
Posts: 6
Thanks all for responding. Then is the study guide incorrect? It says that long is 64 bits, while float is 32. Is that correct?
[ March 10, 2005: Message edited by: Elaine Ware ]
Ren� Star
Greenhorn

Joined: Feb 24, 2005
Posts: 13
Indeed, long is 64 bits, float 32. BUT widening/narrowing is not only about the number of bits. It is about if you can lose information when converting.
And when you convert from a floating type value (float or double) to an integer type, the decimals get cut off - loss of info.

Look at the list of types below. All conversions along the arrows from left to right are defined as widening conversions. All other conversions between non-equal types are narrowing conversions and require a cast.

This is the list you should learn/use for determining if casts are needed, not the actual size in bits of the types.



Note: the following requires a cast, even though char has more bits than byte.

Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
It's not about the number of bits, or about possible loss of precision. It's all about the possible range of values that can be stored.

Even though float is only 32 bit and long is 64 bit float is capable of holding a wider range of values. It is possible to lose precision when putting the value of a long into a float, but the conversion is allowed without a cast.

A long has the range of:
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

while a float has the range of:
1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).

So the float is 'wider' than the long.
Elaine Ware
Greenhorn

Joined: Mar 10, 2005
Posts: 6
Thanks, everyone. I am very clear on this topic. I didn't memorize the 'widening conversion' chart past int.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Legal Data Types
 
Similar Threads
one question about casting
float or double
How to check entered value is of which primitive type?
Argument Scope
Primitive Casting Exercise