Size of primitive types platform vs language

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • APmore
    New Member
    • Jul 2007
    • 14

    Size of primitive types platform vs language

    Hi all,
    I am new to programming.I read the following lines:

    "Size of primitive types in Java is defined in the language specification, whereas in C and C++ it depends on the platform"

    Can somebody explain this clearly?

    thanks in advance,
    Kishore
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    In C, a short is at least 16 bits, but it could be more, but it can be no bigger than a long. A long is at least 32 bits, but it could be more. An int is at least as big as a short, but no bigger than a long. There are other rules of this sort. The point is that the C Standard sets minimum sizes for the primitive types and requires certain relationships between their sizes but it deliberately allows compilers to tune the sizes to make best use of the underlying hardware platform.

    C++ has similar rules but I don't know if the minimum sizes are precisely the same as for C.

    Comment

    • APmore
      New Member
      • Jul 2007
      • 14

      #3
      Originally posted by donbock
      In C, a short is at least 16 bits, but it could be more, but it can be no bigger than a long. A long is at least 32 bits, but it could be more. An int is at least as big as a short, but no bigger than a long. There are other rules of this sort. The point is that the C Standard sets minimum sizes for the primitive types and requires certain relationships between their sizes but it deliberately allows compilers to tune the sizes to make best use of the underlying hardware platform.

      C++ has similar rules but I don't know if the minimum sizes are precisely the same as for C.
      many thanks for the good explanation.

      ......but it deliberately allows compilers to tune the sizes to make best use of the underlying hardware platform.

      What is seen in hardware platform to set the sizes of data types?..like opcode size or memory ..?

      And what about java?how come its machine independent?

      Regards,
      Kishore

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Normally part of the hardware platform that determines these sizes are address bus width, internal register size (and instruction set) and the memory layout. The internal register size and the memory layout are often related to the data bus width.

        The register size(instructio n set) sets the size of the integer that the platform works best with, that is the integersize it loads and saves quickest and can most easily perform arithmetic on. This often has a direct effect on the size of int in C. The memory layout has a direct effect on the size of char, specifically the number of bits in a char. Finally the address bus width has a direct relation to the size of pointers used in C for the platform.

        Comment

        • jfwfmt
          New Member
          • Nov 2009
          • 12

          #5
          Java is a language that (0riginally) compiled into byte codes which are opcodes for a specified (non-existant) machine. The machine in implemented in a Java Virtual Machine. This machine has a defined length of the integers, characters .... The idea was write once, run on many different machines with no changes to the byte codes, just to the JVMs.

          /s/ Jim WIlliams

          Comment

          Working...