• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data types back ward compatability

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear rob

Can you confirm me that in 32 bit system what is the size of short and double
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya of i agree with you but how come the 64 bit long or double number will fit into 32 bit system
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic