aspose file tools*
The moose likes Java in General and the fly likes Data types back ward compatability Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Data types back ward compatability" Watch "Data types back ward compatability" New topic
Author

Data types back ward compatability

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all

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


Creativity is nothing but Breaking Rules
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

You don't need to cast. In Java, int and float are always 32 bit, and long and double are always 64 bit. That's guaranteed by the Java Language Specification.

Internally the JVM may need two native longs to store one Java long, but in your Java programs you don't need to worry about that.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear Rob

please go through my question carefully


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... ina 32 bit system

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

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.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear rob

Can you confirm me that in 32 bit system what is the size of short and double
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

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.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Ya of i agree with you but how come the 64 bit long or double number will fit into 32 bit system
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13863
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3153
    
  10
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.


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data types back ward compatability
 
Similar Threads
Range of integer types
long with l , float with f, double with d
casting variables
Narrowing Problem
Why 100 * A * B * C is not same as A * B * C * 100 if A, B, C are double?