user244333
user244333

Reputation:

What is the significance of class data type in java?

Noob question.

I have been programming at basic level for quite a while but I have trouble understanding class data type.

when we say int a = 9; it means a is of data type int meaning it can contain only integers.

like wise for String, boolean, double, float etc.

But consider the following code:

Class Node {

Node next = null;
int data;

public Node(int d){ data = d; }

void append(int d)
    {
        blah blah blah
        ..............
    }
}

What does Node next = null; mean? I can understand the effort to create an object with

Node next = new Node();

and then try to manipulate the next object.

Upvotes: 5

Views: 16268

Answers (4)

StKiller
StKiller

Reputation: 7951

Node next = null means that this variable is initialized with null value, and doesn't point to any memory location.

So there isn't yest added the next "neighbor" node.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503779

This code:

Node next = null;

declares a variable of type Node. As Node is a class, the value of next is always a reference - either to an object of type Node or a subclass, or the null reference which doesn't refer to any object at all... and in this case the variable starts off with a value of null.

It's really important to understand that the value of next is never a Node object itself... it's only ever a reference. So suppose we have:

Node next = new Node(10);
Node foo = next;

Here, next and foo are separate variables, each with independent values... but we've assigned the value of next as the initial value of foo, which means they both refer to the same object. So if we print out foo.data, it will be 10.

I like to think of variables as pieces of paper - and in the case of variables of reference types, what's written on the piece of paper is the address of a house, or the word "null". If two pieces of paper have the same address written on them, they refer to the same house - but the two pieces of paper themselves are independent. Changing the value of one variable (crossing out the current address and writing another one) doesn't change anything about the other variable... but changes to the house itself (e.g. painting a door red) are visible whichever piece of paper you use to get there.

Note that in your question, you've lumped String in with int, double and boolean... but whereas int, double and boolean are primitive types (where the value of a variable is simply the data itself - the number etc), String is a class, so it's a reference type. The value of a string variable isn't the text itself, but a reference.

Upvotes: 7

Heisenbug
Heisenbug

Reputation: 39204

Node next = null; is a sort of pointer to a NodeObject. It's like declaring a variable not still initialized. In this case the object will probably be instantiated later. If you put an already existing object to a null value, if there aren't other references to the same object, then the garbage collector deletes it.

I have been programming at basic level for quite a while but I have trouble understanding class data type. when we say int a = 9; it means a is of data type int meaning it can contain only integers. like wise for String, boolean, double, float etc.

Pay attention that int and Integer are different in Java. int is a primitive type and it isn't an object. Integer is an object you can instantiate with new operator.

Upvotes: 0

Howard
Howard

Reputation: 39217

The line

Node next = null;

means that you define a variable next which holds only references to objects of type (the class) Node. Additionally you initialize it with the null value which means no object created yet.

Those variables actually hold references to objects and null is a special value valid only for objects which indicates that your variable is empty. This something like next.append() will fail with a nullpointer exception if next is still null.

Upvotes: 3

Related Questions