What is the value of a class data type in java?

Question Noob.

I've been programming at a basic level for quite some time, but it's hard for me to understand the data type of the class.

when we say int a = 9; this means that there is an int data type, meaning that it can only contain integers.

how 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 Node next = null; ? I can understand how to create an object using

 Node next = new Node(); 

and then try to manipulate the next object.

+4
source share
4 answers

This code:

 Node next = null; 

declares a variable of type Node . Since Node is a class, the value of next always a reference - either to an object of type Node , or to a subclass, or to a null reference, which does not apply to any object at all. and in this case, the variable starts with null .

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

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

Here next and foo are separate variables, each of which has independent values ​​... but we have assigned the value of next as the initial value of foo , which means that they both refer to the same object. Therefore, if we type foo.data , it will be 10.

I like to think of variables as separate papers, and in the case of variable reference types, what is written on a piece of paper is the address of the house, or the word "zero". If the same address is indicated on two sheets of paper, they refer to the same house, but the two pieces of paper themselves are independent. Changing the value of one variable (crossing the current address and recording another) does not change anything with respect to another variable ... but changes in the house itself (for example, the door paint is red) are visible regardless of what sheet of paper you use to get there.

Please note that in your question, you included String with int , double and boolean ... but whereas int , double and boolean are primitive types (where the value of the variable is just the data itself - number, etc.), String is a class, so it is a reference type. The value of the string variable is not the text itself, but a link.

+4
source

Line

 Node next = null; 

means that you define the next variable, which contains only references to objects of type (class) Node . In addition, you initialize it to null , which means no object created yet .

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

+3
source

Node next = null means that this variable is initialized to zero and does not indicate any memory location.

So, there is no yest added to the next "neighbor" node.

0
source

Node next = null; is a kind of pointer to a NodeObject. This is like declaring a variable not yet initialized. In this case, the object is likely to be created later. If you put an existing object in the null value, if there are no other references to the same object, the garbage collector will delete it.

I've been programming at a basic level for quite some time, but it's hard for me to understand the data type of the class. when we say int a = 9; this means that there is an int data type, meaning that it can only contain integers. how wise for String, boolean, double, float, etc.

Note that int and Integer are different in Java. int is a primitive type and not an object. Integer is an object that you can create using the new operator.

0
source

Source: https://habr.com/ru/post/1347671/


All Articles