Type System in Java/Python/Go/Javascript
Type determination
- java interface query:
- interfaceDemo
instanceof
InterfaceDemo - subClassInstance
instanceof
ParentClass
- interfaceDemo
- python
- isinstance
- golang interface query:
- value, ok := element.(T)
element is interface var, T is base type
- switch value := element.(type)
- value, ok := element.(T)
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)
1 | /*** java ***/ |
1 | ### python ### |
1 | /*** go ***/ |
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
1 | None, 0, "", u"", list(), tuple(), dict(), set(), frozenset()]) map(bool, [ |
- 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
1 | demoInt := 2 |
- 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
- Object
- primitive type
1 | var n = 1, b = true, s = "test" |
- check by typeof()
- Object type (wrapper for primitive) has methods, except null and undefined
版权声明:本博客所有文章除特殊声明外,均采用 CC BY-NC 4.0 许可协议。转载请注明出处 Kasper Deng的博客!