-----------------------------------------------------------------------------------
---------------------------------------------------
Lets assume that we have a class called Employee in Java. This is how you would
create a reference of the Employee class:
Create a reference
Employee e;
Now if you wanted to create an instance or object of the Employee class, you would
do so:
This gives a direct answer to your question, but to understand the real difference
between references and objects, you need to understand a little about how memory is
managed in Java
In java, all types are divided into two categories, primitive types which include
int, long, char, boolean and so on, and reference types like String, Date,
BufferedReader and other classes. This difference is based on the manner in which
memory is allocated to the variables of these types. For example,
int b;
int a = 10;
Both these statements cause the same effect, memory wise. A block of memory from
the stack, big enough to hold an int (4 bytes) is allocated to these variables.
This memory is allocated statically, since the size of int and other primitive
types is fixed. (The ??? in the diagram indicate some unknown value since the
integer has not been initialized yet)
Employee e;
Employee emp = new Employee();
These two statements have different effect memory-wise since they are reference
types. The first statement just creates a reference, or a pointer if you will, to
an instance of type Employee. This essentially means, the statement tells the
compiler "e" is going to point (refer) to an Employee object, but right now is
pointing to nothing (null). The interesting part is, the reference itself, that is
"e" is stored on the stack statically. So, here you create just the reference.
The second statement however, does more than this. "emp" is allotted memory as a
reference as in the previous case, but the use of new keyword creates an object and
allots memory to it on the heap, at runtime, i.e dynamically. This statement tells
the compiler "emp" is going to refer to the Employee object that will be created
as a result of the new keyword. So, here you create a reference and an object that
the reference variable refers to.
-----------------------------------------------------------------------------------
-----------------------------------------------
MyClass obj1 = null;
A reference variable named obj1 of type MyClass refers to nothing(NULL).
testList.add(obj1);
List testList's first element is assigned the same reference which obj1 holds
currently viz. NULL.
-----------------------------------------------------------------------------------
------------------------------------------------------------
package ds.singlelinkedlist;
----------------------------------------
package ds.singlelinkedlist;
public singlyLinkedList() {
System.out.println();
--------------------------------------------------------
package ds.singlelinkedlist;
public class App {
SinglyLinkedList mylist = new SinglyLinkedList();
mylist.insertFirst(100);
mylist.insertFirst(50);
mylist.insertFirst(99);
mylist.insertFirst(88);
mylist.insertLast(999999999999999);
mylist.displaylist();
}
}
output
-----------------------------------------------------
List(first --> last)
{100}
{50}
{99}
{88}
=============================================================
package ds.circularlinkedlist;
public CircularLinkedList() {
first = null;
last = null;
}
private boolean isEmpty() {
return first=null;
}
if(isEmpty()) {
last = newNode;
}
newNode.next = first; // newNode --> old First
first = newNode; // first Place
}
if(isEmpty()){
first = newNode;
} else {
last.next = newNode; // next value of the last node will point to the new node
last = newNode; // we make new node we created be the last one in the list
}
}
if(first.next == null) {
last = null;
}
first = first.next; // first will point to old's next value
return temp;
}
----
package ds.singlelinkedlist;
public class App {
CircularLinkedList mylist = new CircularLinkedList();
mylist.insertFirst(100);
mylist.insertFirst(50);
mylist.insertFirst(99);
mylist.insertFirst(88);
mylist.insertLast(999999999999999);
mylist.displaylist();
}
}
output
-----------------------------------------------------
List(first --> last)
{88}
{99}
{50}
{100}
{999999999}
=============================================================
Double LinkedList
package ds.doublelinkedlist;
----
package ds.doublelinkedlist;
public DoubleLinkedList() {
this.first = null;
this.last = null;
}
if(isEmpty()) {
last = newNode; // if empty, last will refer to the new node being created
}else{
first.previous = newNode;
}
newNode.next = first; // new node's next field will point to old first
this.first = newNode;
}
if(isEmpty()) {
first = newNode;
}else{
last.next = newNode; // assign old last to new node
newNode.previous = last; // the old last will be the new node privous
}
} else {
first.next.previous = null; // list first node will point to null
}
first = first.next; // assigning refrence of the node following old first node to
the first field in the LL object
return temp; // return deleted old first node
}
// assume non-empty list
}
last = last.previous;
return temp;
}
// assume non-empty list
if(current == last) {
current.next = null;
last = newNode;
} else {
newNode.next = current.next; // new node's next field should point to the node
ahead of the current
current.next.previous = newNode; //the node ahead of current, it's previous field
should point to new node
newNode.previous = current;
current.next = newNode;
return true;
}
}
if(current == last){
last = current.previous;
} else {
current.next.previous = current.previous;
}
return current;
}
------
package ds.doublelinkedlist;
output