Java Type

作者: sunboximeng | 来源:发表于2018-04-20 10:18 被阅读12次

    Everything inside a computer is a number. Specifically, this means that everything is stored in the computer's memory as bits, ones and zeroes.

    But not all numbers mean the same thing. Some numbers might mean plain numbers, while others might mean letters, and others might mean the locations of data in the computer's memory.

    So, the type of a value specifies how to interpret those numbers. It tells Java how to assign meaning to the ones and zeros stored in memory.

    Int x.png Float x.png String x.png

    The Java programming language is a statically(静态表示编译期)typed language, which means that every variable and every expression has a type that is known at compile time. 编译期间类型就是明确的。

    The Java programming language is a strongly typed language in that:

    • Types limit the values that a variable can hold or that an expression can produce. Strong static typing helps detect errors at compile time.
    • Types limit the operations supported on those values. 因此接口、抽象类都是类型,因为他们赋予了operation。
    • Types determine the meaning of the operations. 加法即可以表示数字相加,也可以表示字符串的连接。

    Type分为primitive types and reference types

    Primitive types (8种)

    • 逻辑类型

    • 数值类型

      • 整型
        • byte:字节本节,可表示-128~127之间的数。存储介质可以看成纸带,字节就是纸带上的一个格子。随着机器内存增大,这样的类型很少用了。
        • short ( 2 byte ), int ( 4 byte )(记忆:雷音4), long ( 8 byte )(记忆:浪8你就)
        • char ( 2 byte )(记忆:短(short)裤char有2个裤腿)
            1. Both char b = 65 and 'B' > 'A' are legal.
            1. char可以和数字相加,输出为数字(因为数字不一定能转化成为char)。
            1. Java里面的才是2 byte,所以可以赋值一个汉字。C语言里面是一个字节,就不行。
      • 浮点型:float(4 byte), double(8 byte)
        注意:3 - 2.6 == 0.4 由于精度问题,会返回false。

      注意byte,short,char的变量不会相互转换,在参与运算时,都会自动将类型提升为int类型,其他类型进行混合运算时都是将小的数据类型提升为大的。

      public static void main(String[] args) {
        byte a1 = 4;
        byte a2 = 3;
      // 由于是变量在运算,运算结果不一定是byte,需要强转。
        byte a3 = (byte) (a1 + a2);
      // 由于是常量在运算,编译器可以推断出结果是在byte范围内,于是自动就转型了。
        byte a4 = 4 + 3;
      }
      

    正负数各占一半的内存,也就说符号要占一位。
    随着机器内存增大,byte很少用了,表示的范围太小了。
    字符个数、游戏中道具个数用int就太多了,会上亿。
    IPv4也是4个字节。

    字符 + 数字0:输出字符对应的编码,实现了char --> int。
    String + 数字:执行字符串连接,输出String。
    reference + String:will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference).

    类型转换
    隐式转换:从小到大。
    整数默认int,位数太长需要加L,否则就提示越界了。小数默认double,赋值给float类型的变量需要加f,以明确可以损失精度。
    强制类型转换的原理:截取相应长度的位数。
    缺少一个例子:自动装箱

    类型转化.png
    String --> int:Integer.parseInt()
    int --> String:String.valueOf()

    Reference types

    • Class types
    • Interface types
    • Array types
    • Type variables (泛型里面的类型变量)

    A type variable is introduced by the declaration of /a type parameter of a generic class, interface, method, or constructor/. (句子里面Of多,就是嵌套,就需要考虑一下结合律)
    The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.

    The class Object is a superclass of all other classes. Its main methods are:

    • clone():used to make a duplicate of an object.
    • equals():defines a notion of object equality, which is based on value, not reference, comparison.
    • getClass():returns一个类型名。
    • hashCode():returns a numeric representation of the object.
    • toString():returns a String representation of the object.

    相关文章

      网友评论

          本文标题:Java Type

          本文链接:https://www.haomeiwen.com/subject/atqakftx.html