Discussion 2

Primitive Types vs Reference Types:

the former stands for value, the latter stores the address

e.g.

int x = 10;

In the diagram it looks like: x |__10

Dog x = new Dog ...

x |__ ----> Area of Instance Dog

So when we try to compare two data, most of the time we use the operator"=="

It compares the information at the location of the variable, so it only works for the primitive types ( otherwise it makes no sense comparing two addresses )

However, Java enables its users to check whether the memory addresses of two objects. You just need to use .equals( )

e.g. a,b are two arrays, we can use this code a.equals(b) to compare the two

 

Pass by value:

In a function, when the parameters are sent to the function, they always follow the 'pass by value' 

...
int x=5;
int [] arr = int []{1,2,3,4};
doSomething(x, arr);
public void doSomething (int y, int[] a){
    y = 16;
    a[2] =4;
}

By viewing the code above, can you draw the diagram?

 

Arrays:

Arrays are lists of items that can be indexed into a bracket notation.

Instantiation:

  int [] x =new int[3];

                (it regulates the the size)

Linked Lists:


Linked lists are modular lists that are made up of nodes that each contain a value and a pointer to the next node.

To access values in a linked List, you must use dot notation.

Instantiation:

  IntList intList = new IntList():

So they can be extended or shortened by changing the pointers of its nodes (unlike Arrays)

They can't be indexed directly into like an array, instead the coomputer has to iterate through all of the nodes up to that point and follow their next pointers.

 

Scope and Method:

Instance methods can only be called on Instances

Static methods can be called either instances or class itself, but it can only change the static data.

  

posted @ 2021-01-26 23:38  M1stF0rest  阅读(34)  评论(0)    收藏  举报