Link

Primitive types

Table of contents

  1. What are the primitive types?
  2. What are the wrapper types?
  3. Does Java support unsigned integrals?

What are the primitive types?

Java has eight primitive types and no more can be added.

TypeSizeExampleRange
byte1 byte123 0173 0x7B-128 .. +127
short2 bytes32000 0173 0x5000-32768 .. +32767
int4 bytes70000 010000 0xFFFFFF+- 2147483647
long8 bytes1L 0173L 0x7BL+-9223372036854775807
charUnicode: 2 bytes‘A’ ‘\t’ ‘\u0065’‘\u0000’..’\uFFFF’
float4 bytes123.0F 1.23E2F3.40282347E+38F
double8 bytes123.0 1.23E21.79769313E+308
boolean1 bytetrue falsetrue - false

The primitive types in Java, are all in lower case. It is an int and not Int.

Note that the String type is not in the above list.

What are the wrapper types?

🚧 Pending 🚧

Does Java support unsigned integrals?

Java 8 introduces unsigned int and long as shown in the following example.

package demo;

public class App {
  public static void main( final String[] args ) {
    long unsignedLong = Long.parseUnsignedLong( "18446744073709551615" );

    System.out.printf( "The primitive type: %d%n", unsignedLong );
    System.out.printf( "Using the wrapper functions: %s%n", Long.toUnsignedString( unsignedLong ) );
  }
}

Note that we need to go through the respective wrapper class in order to obtain the unsigned number. The wrapper classes have added a set of methods to handle unsigned version as shown next.

  1. compareUnsigned()
  2. divideUnsigned()
  3. parseUnsignedLong()
  4. remainderUnsigned()
  5. toUnsignedString()

Note that the above methods needs to be used to perform any simple operaiton on the unsigned version of integrals.