I'm using x86 architectute as we know double or long is 64 bit when im using this in my 32 bit processor due to backward compatabality the size of long and double is decreased to 32 bit when i want to assign a long to an int what is the neccesity of type casting any how int is also 32 bit long is also 32 bit...Please give me solution
Well, if you need to put the value of a long into an int, casting is the only way and you will lose information if the long is larger than Integer.MAX_VALUE. That is something that cannot be overcome.
However, why are you using an int? In your Java code you can still use a long, unless you need to go native. Like I said, a Java long is always 64 bits, so if you don't have to switch to int (and like I said, if you don't need to interact with native methods, why would you) then simply don't.
Joined: Apr 06, 2009
Can you confirm me that in 32 bit system what is the size of short and double
In Java code, in any architecture so both 32 and 64 bit, these are the sizes:
- byte: 8 bit
- short: 16 bit
- char: 16 bit
- int: 32 bit
- float: 32 bit
- long: 64 bit
- double: 64 bit
These are guaranteed by the Java Language Specification.
Architecture specific sizes do not occur in Java, unless you are using native methods. The native code (e.g. in C) will use architecture specific sizes. I don't think you'll need to worry about that though.
Joined: Apr 06, 2009
Ya of i agree with you but how come the 64 bit long or double number will fit into 32 bit system
Rob already answered that question in his first post - read it carefully...
32-bit x86 processors have instructions to handle 64-bit longs and doubles. Such numbers don't fit into a normal 32-bit register, so the CPU uses two registers for such numbers.
In C / C++, the size of various data types is system-dependent; an int might be 32-bit or 64-bit, or even only 16-bit on some systems. Java is not the same as C / C++, in Java, the sizes of the data types are always the same, regardless of the processor that the code runs on. If the processor can't handle for example 64-bit numbers directly, then the JVM must simulate it. Section 4.2.1 of the Java Language Specification specifies what value ranges the different integral types can hold.
santhosh.R gowda wrote:Ya of i agree with you but how come the 64 bit long or double number will fit into 32 bit system
Because that's what the JVM specification demands.
How it is actually implemented is up to the designer of the JVM. As long as it is invisible to the end user, they can implement it in whatever way they deem best for the platform they are targetting.