Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between value and reference in a Java exercise?

Tags:

java

class

I am learning Java from Udemy, complete java masterclass. For challenge "Abstract class challenge" narrator states says I should create 2 references and 1 value in an abstract class.

In the solution this is the result:

public abstract class ListItem {
    // references
    protected ListItem rightLink = null;
    protected ListItem leftLink = null;

    // value
    protected Object object;
}

What makes references a reference and not a value?

They both start with protected, then we have type, then we have variable name. The only difference is that reference has assigned value of null.

But if were to make for example:

private int myNumber = 10;

Above is not called a reference, it is called a variable myNumber of type int with value 10.

like image 296
jm18457 Avatar asked Dec 04 '22 20:12

jm18457


1 Answers

It is not a value in the context of the Java language. In the context of the Java language, all three fields are references.

I see it as a value in the context of a linked list. In context of a linked list, an object of type Object is going to be an actual value that is stored in the list. And ListItem is a reference to another list item because that is how linked lists are connected.

Seems like the course material does not make it as clear as it should.

like image 133
Mateusz Avatar answered Feb 11 '23 23:02

Mateusz