JavaRush /Java Blog /Random EN /Reference Data Types in Java

Reference Data Types in Java

Published in the Random EN group
Without understanding Java syntax, it is impossible to become a serious developer, so today we continue to learn the syntax. In one of the previous articles we talked about primitive variables, but since there are two types of variables, today we will talk about the second type - reference types in Java. So what is it? Why are reference data types needed in Java? Reference Data Types in Java - 1Let's imagine that we have a TV object with some characteristics, such as channel number, sound volume and on flag:
public class TV {
   int numberOfChannel;
   int soundVolume;
   boolean isOn;
}
How can a simple type like , intstore this data? Let's remember: one variable intis 4 bytes. But inside there are two variables (4 bytes + 4 bytes) of the same type, and also boolean(+1 byte)... Total - 4 to 9, but as a rule, much more information is stored in an object. What to do? You can't put an object into a variable. At this point in our story, reference variables appear. Reference variables store the address of the memory location in which a specific object is located. That is, this is a “business card” with an address, with which we can find our object in shared memory and perform some manipulations with it. A reference to any object in Java is a reference variable. This is what it would look like with our TV object:
TV telly = new TV();
We set a variable of type TV with a name tellyto a link to the created object of type TV. That is, the JVM allocates memory on the heap for the TV object, creates it and the address to its location in memory, puts it in the variable telly, which is stored on the stack. You can read more about memory, namely the stack and a lot of other useful information, in this lecture . A variable of type TV and an object of type TV, did you notice? This is not without reason: objects of a certain type must have corresponding variables of the same type (not counting inheritance and interface implementations, but now we are not taking this into account). After all, we’re not going to pour soup into glasses, are we? It turns out that our object is a TV, and the reference variable for it is like a control panel. Using this remote control we can interact with our object and its data. For example, set the characteristics for our TV:
telly.isOn = true;
telly.numberOfChannel = 53;
telly.soundVolume = 20;
Here we used the dot operator .to access and begin using the internal elements of the object that the variable refers to. For example, in the first line we told the variable telly: “Give us an internal variable isOnof the object you are referencing and set it to true” (turn on the TV for us).

Redefining reference variables

Let's say we have two variables of a reference type and the objects they refer to:
TV firstTV = new TV();
TV secondTV = new TV();
If we write:
firstTV = secondTV;
this will mean that we assigned the first variable as a value a copy of the address (the value of the address bits) to the second object, and now both variables refer to the second object (in other words, two remote controls for the same TV). At the same time, the first object was left without a variable that refers to it. As a result, we have an object that cannot be accessed, because the variable was such a conditional thread to it, without which it turns into garbage, just lies in memory and takes up space. This object will subsequently be destroyed from memory by the garbage collector . Reference Data Types in Java - 2You can break the connecting thread with an object without another link:
secondTV = null;
As a result, there will be only one link to the object - firstTV, and secondTVwill no longer point to anyone (which does not prevent us from assigning it a link to some object like TV in the future).

String class

Separately, I would like to mention the String class . This is a base class designed for storing and working with data that is stored as a string. Example:
String text = new String("This TV is very loud");
Here we have passed a string to be stored in the object's constructor. But no one does that. After all, strings can be created:
String text = "This TV is very loud";
Much more convenient, right? In terms of popularity of use, Stringit is not inferior to primitive types, but it is still a class, and the variable that refers to it is not a primitive type, but a reference type. We Stringhave this wonderful ability to concatenate strings:
String text = "This TV" + " is very loud";
As a result, we will get the text again: This TV is very loud, since the two lines will be combined into one whole, and the variable will refer to this full text. An important nuance is that Stringthis is an immutable class. What does it mean? Let's take this example:
String text = "This TV";
text = text + " is very loud";
It seems that everything is simple: we declare a variable, give it a value. On the next line we change it. But we don’t really change. Since this is an immutable class, on the second line the initial value is not changed, but a new one is created, which in turn consists of the first + " is very loud".

Reference constants

In the article about primitive types, we touched on the topic of constants. How will a reference variable behave when we declare it final ?
final TV telly = new TV();
You might think that this will make the object immutable. But no, that's not true. Reference Data Types in Java - 3A reference variable with a modifier finalwill be bound to a specific object without the ability to unbind it in any way (redefine it or equate it to null). That is, after setting the value of such a variable, code like:
telly = new TV();
or
telly = null;
will cause a compilation error. That is, finalit only acts on the link, and has no effect on the object itself. If we initially have it mutable, we can change its internal state without any problems:
telly.soundVolume = 30;
Sometimes, variables are designated as final even in method arguments!
public void enableTV (final TV telly){
   telly.isOn = true;
}
This is done so that during the process of writing a method these arguments cannot be overridden and, accordingly, create less confusion. What if we denoted finala reference variable that refers to an immutable object? Eg String:
final String PASSWORD = "password";
As a result, we will get a constant, an analogue of constants of a primitive type, because here we can neither redefine the reference nor change the internal state of the object (internal data).

Let's sum it up

  1. While simple variables store value bits, reference variables store bits that represent how an object is retrieved.
  2. Object references are declared for only one type of object.
  3. Any class in Java is a reference type.
  4. The default value of any reference variable in Java is null.
  5. Stringis a standard example of a reference type. This class is also immutable.
  6. Reference variables with a modifier finalare bound to only one object without the possibility of redefinition.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION