logo

Programming Languages - Types

Last Updated: 2024-01-21

Char in Programming Language

  • Java: 2 bytes, 16 bits, unsigned([0, 65535]), unicode(UTF-16)
  • C/C++: 1 byte, 8 bits, ASCII

Go

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems.

When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

The variable's type can be inferred from the value on the right hand side, depending on the precision of the constant.Check type:

v := 42
fmt.Printf("v is of type %T\n", v)

Type assertion: value.(string) asserts that value is a string, otherwise ok is false

if str, ok := value.(string); ok {
    return str
} else if str, ok := value.(Stringer); ok {
    return str.String()
}

Numerical Types

  • byte: 8 bits, 0xFF
  • short: 2 bytes, 16 bits, 0xFFFF
  • int: 4 bytes, 32 bits, 0xFFFFFFFF
  • long: 8 bytes, 64 bits,

int:

  • 0xffffffff = -1
  • 0x7fffffff = 2147483647
  • 0x80000000 = -2147483648

Java

  • Primitives Types: always have a value, cannot be null, default to false or 0, 0.0f, etc
    • boolean 1
    • byte 8
    • short 16
    • int 32
    • long 64: 1L
    • float 32: 1.0f
    • double 64: 1.0d
  • Reference Types: can be null

Java Byte.toUnsignedInt(): promote byte x to int by adding 1s to the front

public static int toUnsignedInt(byte x) {
    return ((int) x) & 0xff;
}

Primitives

C++

  • Signed values: char, short, int, long, long long
  • Fixed width integer types (since C++11): int8_t, int16_t, int32_t, int64_t
  • Unsigned values: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long
  • Fixed width unsigned integer: uint8_t, uint16_t, uint32_t, uint64_t
  • Floating point values: float, double

Rust

  • Signed: char, i8, i16, i32, i64
  • Unsigned: u8, u16, u32, u64
  • Floating point: f32, f64

Java

  • Signed: byte, char, short, int, long
  • No unsigned values.
  • Floating point: float, double

Max / Min / Infinity

C++

int64_t max_int64_t = std::numeric_limits<int64_t>::max();

double inf = std::numeric_limits<double>::infinity();

Java

jshell> Integer.MAX_VALUE
$1 ==> 2147483647

jshell> Integer.MIN_VALUE
$2 ==> -2147483648
Double.POSITIVE_INFINITY;
Double.NEGATIVE_INFINITY;

JavaScript

> Number.MAX_SAFE_INTEGER
9007199254740991
> Number.MIN_SAFE_INTEGER
-9007199254740991
> Infinity
Infinity
> -Infinity
-Infinity

Python

>>> float('inf')
inf
>>> float('-inf')
-inf

From math:

>>> import math
>>> math.inf
inf

To Binary

Python

Use bin()

>>> bin(124)
'0b1111100'

Format

Java

DecimalFormat df = new DecimalFormat("#.###");
df.format(number);

Scala

scala> "%02d".format(1)
res0: String = 01

Python

>>> "%02d" % 1
'01'

Add Padding

JavaScript

function addPadding(num, width) {
  var str = num.toString();

  while (str.length < width) {
    str = '0' + str;
  }
  return str;
}