File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes conversion of primitives Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "conversion of primitives" Watch "conversion of primitives" New topic
Author

conversion of primitives

Yogesh Mhatre
Greenhorn

Joined: Feb 15, 2001
Posts: 20
from velumurgans notes
-----------------------
widening coversion acepted,narrowing conversions rejected then how come
a 64 bit long fit's in a 32-bit float variable.
long ll = 64;
Float ff = ll; // legal???
Code -->
---------
class q20
{
public static void main(String[] args)
{
System.out.println("Hello World!");
q20 qq = new q20();
qq.callme(7l,7l,7l);
}
void callme(double d, float f, long i){
System.out.println("One");
}
void callme(float ff, double dd , long ll){
System.out.println("Two");
}
void callme(int ff, int dd , int ll){
System.out.println("Three");
}
}
After compilation-->
----------------
q20.java:7: Reference to callme is ambiguous.
It is defined in void callme(double, float, long)
and void callme(float, double, long).
Question -->
------------
According to me the o/p should have been "Two"
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
That is an intelligent question. According to the rules a narrowing conversion is defined as a convertion which loose information of the overall magnitude of the numeric value. The loosing of precision *only* is not considered as a narrowing primitive conversion. The move from long to float will make it to loose the precision but not the magnitude. Java refernce defines only the following 23 conversions as narrowing convertions:
byte to char
short to byte or char
char to byte or short
int to byte, short, or char
long to byte, short, char, or int
float to byte, short, char, int, or long
double to byte, short, char, int, long, or float
Hence a convertion of long to float is automatic and is not considered as a narrowing conversion. This may explain the second question. 7l can be legally converted into a float and hence gives the "ambiguos" compiler error.

------------------
Hari Gangadharan
Out of the turbulance a soft voice spoke to me ...
Son smile and be happy things could be worse...
I smiled and I was happy and things became worse!


<B>Hari Gangadharan</B><BR>Unix is user friendly..<BR>but it chooses to whom it is friendly with!
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
the following comparison:
(Long.MAX_VALUE < Float.MAX_VALUE )
returns true.
this is to say that the same number of bits(64 ie) contain different maximum values.
Could somebody please comment on this?
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
It is a little difficult to explain, but I will try. The confusion is due to the way float and long works.
1. long and int are an integer datatype that means it can store only integers
2. double and float can store decimal data and the data is stored as an exponent and mantissa (?spelling)
So in long the numeric is represented as normal binary representation of the number. In float it is stored as follows
(-/+)A x 10^ (+/-)B (here A is mantissa and B is exponent)
(-/+) is given to indicate a sign byte.
Think like this (The actual numbers may not be correct)-- a bit is kept to store the first sign, next 25 bits are kept to store the mantissa, 1 bit is kept to store sign of exponent and 5 bits for exponents. By storing it in this way it is possible to store values from -1.4 x 10^-45 to 3.4 x 10^38 in a float.
But the disadvantage of float is that it will loose precision when we store huge values. Try this example:
public class Test {
public static void main(String[] args)
{
long li = 922337203685477580l;
float f = li;
long l = (long) f;
System.out.println("Float value "+f+" First Long value = " +li+ " second Long value = " +l);
}
}
The output will come similar to this:
>java Test
Float value 9.2233722E17 First Long value = 922337203685477580 second Long value = 922337217429372928
The precision of long is lost when we moved to float. In other words we can say that the float could store high values by sacrificing the precision.
-- Hope this helps

------------------
Hari Gangadharan
Out of the turbulance a soft voice spoke to me ...
Son smile and be happy things could be worse...
I smiled and I was happy and things became worse!
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
yes hari that's it !
it makes perfect sense.
any pointers to where all of this is explained?
thanks.
Hari Gangadharan
Ranch Hand

Joined: Mar 08, 2001
Posts: 73
I too do not know where you can get these info. Java implemented the IEEE specification of storing floating point and double precision floats. I remembered it since I read it somewhere couple of years back. I will point you if I come across any docs. Meanwhile if you guys find some thing about IEEE standards on storing floats please point it here
------------------
Hari Gangadharan
Unix is user friendly..
but it chooses to whom it is friendly with!
Yogesh Mhatre
Greenhorn

Joined: Feb 15, 2001
Posts: 20
Hari,
But my previous question still remains unanswered can anyone exaplin me why it gave compile time error message(Refer to the code in my first posting above)
Reference to callme is ambiguous.
It is defined in void callme(double, float, long)
and void callme(float, double, long).
ANY CLUES
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
yogesh,
u have passed 3 long parameters in ur call to callme().
long can be upcast to float as well as double, so it
is an ambiguity for the compiler to resolve which of the
2 callme() methods to call ie, the one that takes
float, double, long or the one with double, float, long as arguments because both are valid calls.
another similar eg for such an ambiguous call is:
put the following code in ur main method-
float fl = 2.0f;
double dl = 3;
long ll= 89;
q20 qq = new q20();
qq.callme(fl,fl,ll);
and declare callme() twice as-
void callme(float f, double d, long l)
{System.out.println("f d l");}
void callme(double d,float f, long l)
{ System.out.println("d f l");}
here the compiler would not know which callme() to call as float can be converted to double.
Hari,
The 32 bit IEEE 754 floating point representation used by java is explained at the following link: http://www.cs.umd.edu/class/sum1998/cmsc311/Faq/fp.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: conversion of primitives