permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in implicit/explicit conversions 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 "Doubt in implicit/explicit conversions" Watch "Doubt in implicit/explicit conversions" New topic
Author

Doubt in implicit/explicit conversions

Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
hi,

I came across this question in Dan Chisholm's study guide which provides
chapter-wise questions. Though the answer is correctly provided i am confused.

Can anyone please help?

long is 64 bits, how can it be implicitly converted into a float which is just 32 bits?


class GFC215 {
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
int a1 = 1; long b1 = 2; System.out.print(m(a1)+","+ m(b1));
}}


What is the result of attempting to compile and run the program?
a. Prints: float,float
b. Prints: float,double
c. Prints: double,float
d. Prints: double,double
e. Compile-time error
f. Run-time error
g. None of the above

Answer this is the answer and explanation provided)

2a Prints: float,float

A method invocation conversion can widen an argument of type float to match a method parameter of type double, so any argument that can be passed to m(float i) can also be passed to m(double i) without generating a compile-time type error. For that reason, we can say that m(float i) is more specific than m(double i). Since both methods are applicable, the more specific of the two, m(float i), is chosen over the less specific, m(double i). The arguments of the method invocation expressions, m(a1) and m(b1), are of types int and long respectively. A method invocation conversion can widen an argument of type int or long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i).


Radhika
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Radhika,

Welcome to JavaRanch.

You may need to please adjust your display name complaint to the JavaRanch's naming policy which insists the ranchers to have their first name and a space and their last name. This is to ensure that people use their *real* names and which should not be ficticious.

You can do so by editing your profile.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
Sure...

Sorry that i didn't bother doing it earlier...



Thanks,
Radha Kamesh
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Radhika:
Sure...

Sorry that i didn't bother doing it earlier...


Thank you. No issues.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Radhika:

long is 64 bits, how can it be implicitly converted into a float which is just 32 bits?



Please have a look at this url. Have a look at the chart "Primitive Type Conversion Table".

As per the link,


that the least significant digits may be lost in the conversion even though the target type allows for bigger numbers. For example, a large value in an int type value that uses all 32 bits will lose some of the lower bits when converted to float since the exponent uses 8 bits of the 32 provided for float values.


The same holds good when a long value gets internally converted into float or double.

Does this help?
[ August 17, 2007: Message edited by: Raghavan Muthu ]
Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
That link proved to be really useful !!!


Thanks a bunch
Radha Kamesh
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Radhika:

That link proved to be really useful !!!


You are welcome
 
Don't get me started about those stupid light bulbs.
 
subject: Doubt in implicit/explicit conversions