JavaRush /Java Blog /Random EN /What is a reference data type
cerebellum
Level 7

What is a reference data type

Published in the Random EN group
And so, while studying the Java language, I just can’t understand what a reference type is and why it needs to be specified when declaring an instance of a class. With the class String, for example, everything seems clear: What is a reference data type - 1
//Создаётся link типа String
String s = "Ссылка на an object, представляющий собой строку";
But all clarity disappears when a similar approach is used: Let's say there are two classes: A (с методом a1)and B (с методом b1).
class A
{
    public static void a1()
    {
        ///
    }
}

class B
{
    public static void b1()
    {
        ///
    }
}
There is also a class with maina method in which, for example, an object of the class is created AB.
class GetTypeVar
{
    public static void main(String[] args)
    {
        A obj1 = new A();
        B obj2 = new B();
    }
}
So, actually, what is the misunderstanding:
  • What does the type of the variable being created mean in this case?
  • What data type do you mean?
It is especially unclear when an object is created like this:
class GetTypeVar
{
    public static void main(String[] args)
    {
        B obj1 = new A();
        A obj2 = new B();
    }
}
That is, a link leading to an instance of one class is set to the class type of another. For what? What does this give? In general, the creation of this topic was inspired by thoughts in search of truth. I will be glad to hear quality parting words...
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION