According to the Java language specification, int to float and long to float is also widening primitive conversion, though it may cause some loss of precision.
May be you would like to have a look here for further detail.
MooN
Collins Mbianda
Ranch Hand
Joined: Aug 11, 2007
Posts: 259
posted
0
Hi !!!
The method m1 is overloaded.
At compile time the best match will be selected.
The method that most closely matches the given parameter is selected. If the compiler cannot select one of the two methods as a better match than the other, the method selection process will fail and the compiler return a message of error.
I think it's for that reason that m1(float i) is selected. But i don't understand why it is chosen also for a double(wich is wide than a float). Do any one has the reason ? [ August 21, 2007: Message edited by: Collins Mbianda ]
SCJP 5.0 | SCWCD 1.4
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
posted
0
Hi Collins,
But i don't understand why it is chosen also for a double(wich is wide than a float). Do any one has the reason ?
But in that code doesn�t have the line obj.m1(d);//double. But if you put in then the result is "inside double param".
Alexsandra
Collins Mbianda
Ranch Hand
Joined: Aug 11, 2007
Posts: 259
posted
0
Hi Alexsandra !!!
You are wright when you say that:
But in that code doesn�t have the line obj.m1(d);
I make a mistake reading the code.
Thanks But i'm not agreing with you when you say:
But if you put in then the result is "inside double param".
The result will be: "inside float param ".
This because "between two methods that accept float or double, float will be chosen". [ August 21, 2007: Message edited by: Collins Mbianda ]
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
posted
0
Hi Collins,
This because "between two methods that accept float or double, float will be chosen".
Where did you read this? Is this valid to double variables either? I think isn't because I have tested and the result really is "inside double param" when I write obj.m1(d);//double...
You are wrigt. I first run the code i wrote this morning.
The result is "inside double param" What i read on the mock exam is false:
...between two methods that accept float or double, float will be chosen
It turn coherent to me now. Many thanks [ August 21, 2007: Message edited by: Collins Mbianda ]
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
posted
0
Hello Collins!
Thanks for your reply!
suneel kumar
Ranch Hand
Joined: Jan 08, 2007
Posts: 46
posted
0
Hi , In the above code methods are m1(float i) & m1(double i) If we pass values except matching datatypes then first method (mi(float i ) will execute.
Here is the code :
package com; public class WideningTest { void m1(float i){ System.out.println("inside float param"); } void m1(double i){ System.out.println("inside double param"); } public static void main(String []args) { int i = 1; short s = 2; byte b = 3; char c = 4; long L = 5; float f = 4; double d = 9; WideningTest obj = new WideningTest(); obj.m1(i);// int obj.m1(s);//short obj.m1(b);//byte obj.m1(c);//char obj.m1(L);//Long obj.m1(f);//float obj.m1(d);//double
long converts to float implicitly because you can represent a long value in a float in different way using scientific notation though long is 64 bit and float is 32 bit and it may result some loss of precisions.