Primitive vs. Reference Data Types

来源:互联网 发布:淘宝达人如何发帖 编辑:程序博客网 时间:2024/05/21 14:55

转自:http://pages.cs.wisc.edu/~bahls/cs302/PrimitiveVsReference.html

Primitive vs. Reference Data Types

Primitives vs. References

  • primitive types are the basic types of data
    • byte, short, int, long, float, double, boolean, char
    • primitive variables store primitive values
  • reference types are any instantiable class as well as arrays
    • String, Scanner, Random, Die, int[], String[], etc.
    • reference variables store addresses

Assignment

  • copies the contents of RHS variable into LHS variable
    • primitives: the primitive value is copied
    • references: the address is copied
  • implications: for references the object is not copied, it is shared (reference variables are aliases)

Comparisons (e.g. ==)

  • compares the contents of the variables
    • primitives: the primitive values are compared
    • references: the addresses are compared
  • implications: for references the contents of the objects are not compared

Passing Parameters

  • terminology:
    • formal parameter: the parameter variable that is listed (along with its type) in the method declaration
    • actual parameter: the parameter that is given when the method is called
  • copies the contents of actual parameter into the formal parameter (i.e., pass-by-value)
    • primitives: the primitive value is copied
    • references: the address is copied
  • implications: for references the object is not copied, it is shared (i.e., actual parameter and formal parameter are aliases)
  • primitives: changing the formal parameter's value doesn't affect the actual parameter's value
  • references: changing the formal parameter's address doesn't affect the actual parameter's address but changing the formal parameter's object does change the actual parameter's object since they refer to the same object

Returning Values

  • returns a result to where the method was called
    • primitives: the primitive value is returned
    • references: the address is returned
  • recall: local variables and parameters are destroyed when the method finishes execution
  • implications: a locally created object can survive if it is returned or if it is stored in a data member
译文:

原始与参考数据类型

基元与参考

  • 原语类型的基本类型的数据
    • byte, short, int, long, float, double, boolean, char
    • 原始变量存储的原始值
  • 引用类型的实例化类以及阵列
    • String, Scanner, Random, Die, int[], String[]等等。
    • 引用变量存储地址

分配

  • 副本RHS变量的内容到LHS变量
    • 原语:原始值被复制
    • 引用:地址复制
  • 影响 :为参考对象是不可复制的,它是共享的(引用变量是别名 )

比较(如==)

  • 比较变量的内容
    • 原语:原始值进行比较
    • 引用:地址比较
  • 影响 :为参考对象的内容不相

传递参数

  • 术语 :
    • 形式参数:即在方法声明中列出(连同其类型)的参数变量
    • 实际参数:即当调用该方法给出的参数
  • 副本实际参数的内容到形式参数(例如, 通过按值 )
    • 原语:原始值被复制
    • 引用:地址复制
  • 影响 :为参考对象是不可复制的,它是共享(即实际参数和形式参数是别名)
  • 原语 :改变形参的值不会影响实际的参数值
  • 引用 :改变形式参数的地址不影响实际参数的地址, 改变形式参数的对象不改变实际参数的对象,因为它们指的是同一个对象

返回值

  • 返回一个结果给其中该方法被称为
    • 原语:返回原始值
    • 参考文献:地址返回
  • 记得:局部变量和参数被破坏时,该方法执行完毕
  • 影响 :如果返回它本地创建对象可以存活,或者如果它被存储在一个数据成员

0 0
原创粉丝点击