int

Type Range Default Value
int -2,147,483,648 to 2,147,483,647 0

The int type represents a 32-bit signed two's-complement integer. The int type can also use the alias signed int.

Literals

Integrals without a suffix are interpreted in this order: int, unsigned int, long, unsigned long, byte, signed byte, short, unsigned short, and char. If the integral value does not "fit" within any specified types, such as possible function overloads or a specified variable type, an error will be raised at compile time.

There is no suffix for the int type, and integer literals that fit within the range of the int type can be written without a suffix.

Auto-boxing

All primitive types have a corresponding wrapper class. The corresponding wrapper class for the int type is System.Integer32.

Auto-boxing occurs when a value of a primitive type is assigned or passed as an argument to a variable or function that expects a value of its corresponding wrapper class. For example, the integer literal 100 is interpreted as int and can be assigned to a variable of type System.Integer32 via auto-boxing:

1
System.Integer32 value = 100; // Auto-boxing

Without auto-boxing, the equivalent code would be:

1
System.Integer32 value = new System.Integer32(100);

The reverse also occurs. Unboxing is when the wrapper class is automatically converted to its corresponding primitive type. Thus, an instance of System.Integer32 can be unboxed to the int primitive type.

1
int value = new System.Integer32(100); // Unboxing

Overflow and Wrapping

When values for integer types go out of range at runtime, they will "wrap around". Thus, if a variable is declared as having the int type, and, at runtime, the variable's value exceeds the int maximum (2,147,483,647) by one, it will wrap around to the int minimum (-2,147,483,648); if the value exceeds the int maximum by two, it will wrap around to the int minimum plus one (-2,147,483,647); and so on.

Conversions for Internal Types

For conversions to and from the int type, consult the Conversions Table.

Conversions for external Type

When converting from external:

  • If the external value is a numeric value, it will be automatically converted to the equivalent integer value.

  • If the external value is a numeric value that is out of range, wrapping will occur.

  • If the external value is a numeric string value, it will be automatically converted to the equivalent integer value.

  • If the external value is a numeric string value that is out of range, wrapping will occur.

  • If the external value is a non-numeric string value, it will be automatically converted to zero (0).

  • If the external value is the Boolean true value, it will be automatically converted to one (1).

  • If the external value is the Boolean false value, it will be automatically converted to zero (0).

  • If the external value is a non-numeric value and non-Boolean value, it will be automatically converted to zero (0).

When converting to external, the value will be converted to a JavaScript number value.

See Also

Share

HTML | BBCode | Direct Link