aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes primitive widening doubt 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 "primitive widening doubt" Watch "primitive widening doubt" New topic
Author

primitive widening doubt

aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Hi there,



float widens for int,short,byte,char & float ofcourse.
how does it widen for long?

the output for the above...

inside float param
inside float param
inside float param
inside float param
inside float param
inside float param

may I know how it does?


Thanks
regards
samura

Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

Hi Samura Babu

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
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
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
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
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...

Alexsandra
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Witch version of java do you use ?

I test and have: "inside float param". I use the 1.6 version.

I read it on mock exam written by John Meyers(Question 19).

John Meyers Mock exam
Alexsandra Carvalho
Ranch Hand

Joined: Jul 13, 2007
Posts: 75
My version is 1.5...

and now? Somebody explain this?
Collins Mbianda
Ranch Hand

Joined: Aug 11, 2007
Posts: 259
Hi Alexsandra !!!

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
Hello Collins!

Thanks for your reply!
suneel kumar
Ranch Hand

Joined: Jan 08, 2007
Posts: 46
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

}
}
o/p inside float param
inside float param
inside float param
inside float param
inside float param
inside float param
inside double param
Neha Bhattad
Ranch Hand

Joined: Aug 20, 2007
Posts: 30
Long and Double are both 64 bits. I would have thought that for widening long it would choose the method which takes double...

However, It converts to float which is 32 bits and could result in loss of precision.

why?!
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

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.

You may go through this nice article about widening/narrowing conversion.
aslika bahini
Ranch Hand

Joined: Mar 03, 2007
Posts: 111
Thanks to all!

regards
samura
 
Don't get me started about those stupid light bulbs.
 
subject: primitive widening doubt