Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in implicit/explicit conversions

 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure...

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



Thanks,
Radha Kamesh
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Radhika:
Sure...

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


Thank you. No issues.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That link proved to be really useful !!!


Thanks a bunch
Radha Kamesh
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Radhika:

That link proved to be really useful !!!


You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic