File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Doubt in casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in casting" Watch "Doubt in casting" New topic
Author

Doubt in casting

vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
Hello all,

As Iam in starting stage of JAVA ,I got this doubt.Actually tried to point out the reason by myself ,but could not,so taking help from you all.

when Iam trying to assign a double variable(which occupies 64 bits) to a var.of type float(which occupies 32 bits) ,compiler is showing error,but when I cast it,it is not showing any error.But,when Iam trying to assign a long,var.(which occupies 64 bits) to float(which occupies 32 bits) with out casting its not giving any error message.May I know the reason behind this?

I wrote a program like this.


public class castTest
{
public static void main(String arg[])
{
float f=10.3F;
long l=32767;
double d=-2147483648;
System.out.println("before Casting");
System.out.println("float f is "+f);
System.out.println("long l is"+l);
System.out.println("double d is "+d);

f=(float)d;

System.out.println("after assigning double value d to f,new f is "+f);

f=l;

System.out.println("after assigning long value l to f,new f is "+f);

}
}
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, Vats!

You are mentioning how many bytes the different primitive types occupy. But for casting this is not relevant. Only how big the numbers can be.
There is a sequence of this types how they always can be assigned without explicit casting:

byte < short < int < long < float < double

I didn't include char into this row, because when e.g. comparing char and short it is bigger and smaller at the same time. Because char can take higher values than short (so bigger), but short can take negative values, and char can't (so smaller).

When you assign:
type x = value;
type a = x;

If the type of x is smaller than the type of a, you never need an explicit cast.
If it is bigger, you'll normally need one, but there are a few exceptions (e.g. regarding final variables).

Yours,
Bu.


all events occur in real time
vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
Hello Burkhard,

Thank you for your reply,but still doubt.

In your reply you mentioned that the number of bytes is not relevant but only how big the numbers are.

According to the range of their numbers we can mention that float<double (or long<float)? Or anything else?

As you mentioned "long<float" So,no error in assigning long to float.

But where as "float<double". So the statement f=d is giving eroor?

Am i correct in my understanding?
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Hi Vats

Will it be possible for you to pour a Bottle of water (fully) in to a glass ??. It is not possible because it wont be enough for a glass to have the quantity of a Bottle.To do this we have CASTING in Java.

The priority of big numbers are clearly given in the above.

Now you got it i think!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in casting
 
Similar Threads
How does a long fit in a float?
Casting hierarchy
Casting
Assigning long value to float?
Casting