Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

primitive widening doubt

 
aslika bahini
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My version is 1.5...

and now? Somebody explain this?
 
Collins Mbianda
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Collins!

Thanks for your reply!
 
suneel kumar
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all!

regards
samura
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic