This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes code of my previous question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "code of my previous question" Watch "code of my previous question" New topic
Author

code of my previous question

rashid khokhar
Greenhorn

Joined: Mar 12, 2001
Posts: 4
link with long==>float
class myclass{
float i;//float have 32 bits.
long j;//long have 64 bits.
void method(){
j=2043223;
i=j;// convert long into float
System.out.println(i);
}
}
class tester{
public static void main(String agrs[]){
myclass my=new myclass();
my.method();
}
}
output = 2043223.0

------------------
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi Rashid,
quoting Rana Thakur:
Rana Thakur
greenhorn posted February 27, 2001 08:14 AM
--------------------------------------------------------------------------------
Hi,
The value for a long variable will be stored as 64 bits.
However the floating point value will be stored in 32 bits in IEEE floating point format.i.e left most bit for sign bit, next 7 bits from the left for exponent and the remaining 24 bits for mantissa. Thus the value that can be stored using this format(maximum value being 3.4E+038) is much higher than that of the value that can be stored in a simple long variable(max value (2^63)-1). Hence the conversion is widening and not narrowing.

------------------------------------------------------
There was some discussion about this point in the forum. Search for "float".
A float can take up a higher value then a long. The cost is lower precicion.
hope this helps
Axel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: code of my previous question
 
Similar Threads
shift operator
int to float conversion !
Casting
Doubt in Threads
narrowing the reference