Reference And List
Walrus a = new Walrus(100, 8.3);
Walrus b;
b = a;
b.weight = 5;
System.out.println(a);
System.out.println(b);
result ={a.weight = 5 b.weight = 5}
VS
int x = 5;
int y;
y = x;
x =2;
System.out.println("x is:" + x);
System.out.println("y is:" + y);
result ={x is: 2 y is: 5}
Bits
Your computer stores information in “memory”
- Information is stored in memory as a sequence of ones and zeros
- Example: 72 stored as 01001000 the same as ‘H’
- Example: True stored as 00000001
Each Java type has a different way to interpret the bits:
- 8 primitive types in Java: byte, short, int, long, long, float, double, boolean, char
Primitive Types
When you declare a variable of a certain type in Java;
- Your computer sets aside exactly enough bits to hold a thing of that type
- int —> 32 bits
- double —> 64 bits
- Java creates an internal table that maps each variable name to a location
- Java does NOT write anything into the reserved boxes(boxes是用来存放二进制的)
Reference Types
When we instantiate an Object:
- Java first allocates a box of bits for each instance variable of the class and fills them with a default value(e.g.0, null)
- The constructor then usually fills every such box with some other value
Can think of new as returning the address of the newly created object
- Addresses in Java are 64 bits.
- If object is created in memory location 811617896, then new returs 811617896
000……000[00001111101000][01000011001100110011001100110101]000……000
b和a都存储了同样的01序列,指向了同一个实例对象,所以b.weight的变化会影响a.weight
Parameter Passing
Given variables b and a:
- b = a
copies
all the bits from a into b
Passing parameters obeys the same rule: Simply copy the bits
to the new scope (This is also called by value)
The golden rule:
- b = a
copies the bits
form a into b - Passing parameters
copies the bits
Instantiation of Arrays
Arrays are also Objects. As we’ve seen, objects are(usually)instantiated using the new
- Planet p = new Plant(0, 0, 0, 0, 0, “blah.png”);
- int[] x = int[]{0, 1, 2, 95, 4}; //实际位数要比32*5位要稍微大一些,多出来的空间存储其他信息
int[] a;
- Declaration creates a 64 bit box intended only for storing a reference to an int array. No object is instantiated
- Creates a new Object, int this case an int array
- Puts the address of this new Objects into the 64 bit box named a.
- If we were to reassign a to something else, we’d never be able to get the original Object back!
IntList and Linked Data Structures
Let’s define an IntList as an object containing two member variables
- int first;
- IntList rest;
IntList这个数据结构存在指向的形式,导致读取其上的元素需要一些递归操作,而由于程序员手动递归获取元素,那就很容易出错(“暴露的递归数据结构”)
第一种实现方式:用前一个IntList
public class IntList{
public int first;
public IntList rest;
public static void main(String[] args){
IntList L = new IntList();
L.first = 5;
L.rest = null;
//print out
System.out.println(L.first);
System.out.println(L.rest);
L.rest = new IntList();
L.rest.first = 10;
//print out
System.out.println(L.rest.first);
System.out.println(L.rest.rest);
L.rest.rest = new IntList();
L.rest.rest.first = 15;
//print out
System.out.println(L.rest.rest.first);
System.out.println(L.rest.rest.rest);
}
}
输出结果:
Compiling IntList.java.......
-----------OUTPUT-----------
5
null
10
null
15
null
[Finished in 0.7s]
用Java Visualizer可视化:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6jRE7lzD-1620121620377)(…/…/image-20210504092308079-1620091391446.png)]
public class IntList{
public int first;
public IntList rest;
public IntList(int f, IntList r){
first = f;
rest = r;
}
public static void main(String[] args) {
IntList L = new IntList(15, null);
L = new IntList(10, L);
L = new IntList(5, L);
System.out.println(L.first);
System.out.println(L.rest);
System.out.println(L.rest.first);
System.out.println(L.rest.rest);
System.out.println(L.rest.rest.first);
System.out.println(L.rest.rest.rest);
System.out.println(L.size());
}
}
print out:
Compiling IntList.java.......
-----------OUTPUT-----------
5
IntList@5ca881b5 //应该是内存地址
10
IntList@24d46ca6
15
null
[Finished in 0.7s]
Size
//Return the size of the list using recursion
public int size(){
if( rest == null){
return 1;
}
return 1 + this.rest.size();
}
//Return the size of the list using no recursion
public int size(){
IntList p = this;
int total = 0;
while (p != 0){
total += 1;
p = p.rest;
}
return total;
}
这里遇到了一个关于this指针的问题,参考了csdn上的文章:
this.属性名称
//指的是访问类中的成员变量,用来区分成员变量和局部变量(重名问题)
//传入的参数变量名与类中属性变量名重复,因此我们在set方法和有参构造方法中加上了this.类属性名称,这样就可以完成对 对象变量的赋值
Person(String name,int age,String gender){
this.name = name;
this.age = age;
this.gender = gender;
名称
//指的是访问类中的成员变量,用来区分成员变量和局部变量(重名问题)
//传入的参数变量名与类中属性变量名重复,因此我们在set方法和有参构造方法中加上了this.类属性名称,这样就可以完成对 对象变量的赋值
Person(String name,int age,String gender){
this.name = name;
this.age = age;
this.gender = gender;