Are parameters with vectors in Java always by reference?

Asked

Viewed 223 times

1

I am aware that, in Java, any parameter passage of an object is by reference, already with primitive types, by value. However, I was left with doubt about the common vector. I don’t mean the class Vector, and yes to a simple vector of the type:

 Tipo vetor = new Tipo[Tamanho] 

When passing an array as a parameter in a method am I changing the vector itself or generating a copy? A vector is considered primitive or object (since it points to the address of the first element)?

2 answers

6

I am aware that, in Java, any parameter passage of an object is by reference

Your science is wrong, only objects originating from classes are passed by reference. Objects coming from other forms are also objects. At present only the originating types of classes descend from Object And so maybe some people confuse what is an object. Java made the conceptual mistake (and this is one of the reasons language is criticized) of not treating primitive types as derived types of Object, Since then the language had to create a lot of gambiarra to fix the problems caused by this bad decision. I always talk: conceitue right and your software will survive well for a long time, are not methodologies (Agile), or principles (SOLID), or paradigms (OOP), or approaches (DDD), or design standards or architecture (MVC), much less good practices that make everything work, is always the right concept.

already with primitive types, by value

Yes, this is true, but soon you will have new types that will be passed on by value and probably, this time, they will be inherited from Object.

However, I was left with doubt about the common vector. I don’t mean the Vector class, but a simple vector of the type: Tipo vetor = new Tipo[Tamanho]. When passing a vector as parameter in a method I will be changing the vector itself or generating a copy?

Internally a array is like a class, is a type by reference like any other. All large objects need to be used by reference to be efficient. Again, it was conceptualized wrong and so has some difficulties, but it works.

There will be change of the internal values within it as other types by reference, changed within a method this change will be reflected in the variable that was used to sustain this object.

A vector is considered primitive or object?

He is both things. It is a primitive type (which is different from being by value) and it is an object, which are all types (these are the correct concepts). Besides this he is a type by reference, as I reported before there is the relationship you are thinking there (most people who program in Java understand wrong, and with the evolution already prepared the language will become clearer and will blow the head of many people who learned wrong). In the background this is called primitive object, after all "primitive" is an adjective of some noun, in this case the object hidden noun.

Some people may consider that a primitive is only a typical value that the computer deals with directly, by this definition it is not a primitive, but it gives question that. But many consider that primitives is what is defined by language and not by the library. wikipedia accepts both definitions (which is not to say that it is a correct canonical information, I like more of the definition that is something builtin language), but understanding the definition draws its conclusions (I think you can consider a array as something that the machine also understands, although in a less direct way, after all it is only a pointer to a given). I don’t know how Java will consider, probably we will know this when it implements the types by value defined by the programmer, it will have to define better what are things.

once it points to the address of the first element

This is not true, this is true for C, not for Java.

In general one should avoid the direct use of array and use more abstract classes like the ArrayList for example that does everything that the array makes with advantages. But there are cases for its use, including because of the legacy.

  • Thank you for the reply. It was very good. However, I ended up getting doubts in some points for so much info rs. For example, when distinguishing objects, speaking of those originating from thought classes... "And is there an object without having come from a class? rs" Searching for the post you put as a link, it seems to me that the term object can be used in a context other than OOP, in the sense of being a given... However, I only heard this term in Object Orientation and so it got a little confusing.

  • At least the answer is right and could help you learn right, several people considered it so. What you just said is "I learned wrong and I will continue like this because what matters is what I learned first and not right". But if you wish it’s your right, it’s a shame, the opportunity you had.

2


When passing a vector as a parameter in a method I will be changing the vector itself or generating a copy?

It will change the array itself. The method takes as parameter the address of the array in memory, so you can change the values of the referenced array, but not its reference.

A vector is considered primitive or object?

It is an object. In Java there are classes for each type of array. These classes are outside the scope of the programmer.

Array type             Corresponding class Name
int[]                     [I
int[][]                   [[I
double[]                  [D
double[][]                [[D
short[]                   [S
byte[]                    [B
boolean[]                 [Z

https://www.geeksforgeeks.org/array-primitive-type-object-java/ https://stackoverflow.com/a/14062177/5360385

  • 1

    Thank you, friend. I saw the reference you put and this article on Arrays and primitive type helped in understanding, despite being in English rs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.