type system in Java/Python/Go/Javascript

2015-01-02 KasperDeng 更多博文 » 博客 » GitHub »

programming

原文链接 https://kasperdeng.github.io/TypeSystemInJavaPythonGoJS/
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


Type determination

  • java interface query:
    • interfaceDemo instanceof InterfaceDemo
    • subClassInstance instanceof ParentClass
  • python
    • isinstance
  • golang interface query:
    • value, ok := element.(T) element is interface var, T is base type
    • switch value := element.(type)

Cast

  • golang not support implicit type cast
  • tuncation
    • small type casts to large type is safe
    • large -> small causes tuncations, only keeps the data in low address. The situation depends on what saves in low address (big-endian/little-endian)

{% highlight java %} /*** java ***/ double doubleDemo = 0.1d; int intDemo = (int) doubleDemo ; {% endhighlight %}

{% highlight python %}

python

type(str(123))

bin(17), int('0b10001', 2) oct(20), int('024', 8) hex(22), int('0x16', 16)

suuport string ~ container(list, tuple, dict, set) conversion

str([0, 1, 2]), eval("[0, 1, 2]") {% endhighlight %}

{% highlight go %} /*** go ***/ var int32Demo int32 = 12345 var int64Demo int64 = int64(int32Demo)

switch value := element.(type) { case string: ... case []byte: case int: default: } {% endhighlight %}

Primitive

  • Java:
    • null
    • boolean, byte, char(2 bytes), short, int, long, float, double
    • fraction literal is double by default. e.g. 3.14 is double type
  • Python: everything is object
    • None
    • bool
    • int, long, float, complex
    • sequence: str, unicode, list, tuple
    • dict
    • set, frozenset

{% highlight python %}

map(bool, [None, 0, "", u"", list(), tuple(), dict(), set(), frozenset()]) [False, False, False, False, False, False, False, False, False] int(True), int(False) (1, 0) {% endhighlight %}

  • Go:
    • nil
    • bool
    • int8, uint8, int16, int32, int64, int, uint, uintptr
    • float32, float64
    • complex64, complex128
    • string
    • byte(utf-8), rune(unicode), no char
    • error
    • compound type: array, slice, map, chan, struct, pointer, interface

{% highlight go %} demoInt := 2 if demoInt { fmt.Println("int value can be used by if") } error output: non-bool demoInt (type int) used as if condition {% endhighlight %}

  • Javascript:
    • primitive type
      • undefined: undefined
      • null: null - can be treated as a specifal value of Object
      • boolean: true, false
      • number: double (64bits, 8 bytes), NaN, Infinity
      • string: UTF16
    • object type
      • Object
        • wrapper: Number, Boolean, String

{% highlight javascript %} var n = 1, b = true, s = "test" var Num = new Number(n); var Boo = new Boolean(b); var Str = new String(s); {% endhighlight %}

  • check by typeof()
  • Object type (wrapper for primitive) has methods, except null and undefined