奇思妙想 · 2024年 9月 22日 0

编程语言的基本类型

152 次浏览

编程语言基本类型大同小异,有符号和无符号也是类型修饰符,但不改变类型主体含义,为避免列出类型名太多,喧宾夺主,如下先忽略有无符号类型的区别。

  • C/ObjC/C++
    char / short / int / long (long) / float / (long) double
    • C++: bool
      string是STL类,不算基本类型
    • C99引入_Bool,C++也可用,本质上_Bool和bool都被当做整型。
    • ObjC 额外提供BOOL等类型。
  • Fortran
    integer / real / complex / character / logical
    • kind参数可指定浮点数精度:real(kind=4)和real(kind=8)分别是32位和64位浮点数。
  • 仓颉
    Int8 / Int16 / Int32 / Int64 / IntNative / Float16 / Float32 / Float64 / Rune(字符类型) / String / Bool / 元组 / 区间 / Nothing / Unit
    • Rune代表Unicode字符,早期曾使用Char类型,此类型会逐渐phase out.
    • Unit类型是一种避免一些副作用表达式有返回值的做法。例如i++在C语言的返回值也是i类型,但仓颉会把它当做Unit类型,即 () 类型,本质是编译器想要程序员不要用这种类型。
    • Nothing类型是所有类型的子类型,没有任何值。
  • JavaScript
    五种基本类型:Undefined、Null、Boolean、Number、String
    • Undefined类型类似于C语言的未声明或者声明未初始化的变量
    • Null类型有唯一字面量null, 和C#或Java的null类似,本质是object类型
  • Rust
    bool / char / i8 / i16 / i32 / i64 / i128 / f32 / f64
  • Go
    整型、布尔、字符串类型
    int / int8 / int16 / int32 / int64 / byte / rune / float32 / float64 / complex64 / complex128
    • byte 等同于uint8, rune 等同于int32
  • C#
    基本类型又被称为预定义类型或基元类型,基类型都有短名称和完整名称(对应BCL)。
    byte short int long float double decimal bool null void
  • Pascal
    • 整型 (byte word shortint integer longint) 实数 (real single double extended comp)
      real类型可以运行在软件仿真环境,其它实数类型需要运行在处理器浮点模式。
    • 布尔 boolean 字符 char
  • PHP
    • integer float/double boolean string
  • Kotlin
    • Kotlin不再区分基本类型和引用类型,任何类型都继承Any类型。可以被认为是"基本"类型有如下:
      • 数值类型、布尔类型、字符
        • Byte Short Int Long Float Double Boolean Char
        • 注意:这些类型均继承Any类,例如,都可以调用Any类hashCode方法。
  • VB
    • Byte Integer Long Single Double Boolean String Currency Date Object Variant
      • Byte是无符号1字节,Integer和Long分别是2字节和4字节有符号数,Boolean是2字节。
      • Single是4字节,Double是8字节。
  • VB.NET
    • 除了VB的类型,VB.NET引入了SByte, Short, UShort, ULong, UInteger等类型,为了适配.NET平台。

类型标志

  • VB是鲜有的提供类型标志的语言,例如%代表Integer类型,#代表Double类型。
    • 90和90%同样代表整型90.