Parameters with vectors in Java are always by reference?

I am aware that in Java, any parameter pass of an object is by reference, already with primitive types, by value. However, I was in doubt about the common vector. I do not mean the Class Vector, but a simple vector of the type:

 Tipo vetor = new Tipo[Tamanho] 

When passing a vector as a parameter in a method will I be 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)?

Author: Maniero, 2019-07-12

2 answers

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

Will change the array itself. The method receives as a parameter the address of the array in memory, so you can change the values of the referenced array, but not its reference.

Is a vector considered primitive or object?

Is an object. In Java there are classes for each array type. These classes are outside the scope of the developer.

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

 2
Author: renanvm, 2019-07-12 13:02:04

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

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

Already with primitive types, by value

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

However, I was in doubt about the common vector. I am not referring to the vector class, but to a simple vector of type: Tipo vetor = new Tipo[Tamanho]. By passing a vector as well as parameter in a method I will be changing the vector itself or generating a copy?

Internally a array is like a class, it 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 internal values within it like other types by reference, changed within a method this change will be reflected in the variable that was used to sustain this object.

Is a vector considered primitive or object?

He is both. 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). In addition to this it is a type by reference, as I reported before there is the relationship that is finding there (most people who Program in Java understand wrong, and with the already prepared evolution of the language this will become clearer and will explode the head of many people who learned wrong). Basically this is called primitive object, after all" primitive " is an adjective of some noun, in this case of the hidden object noun.

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

Since it points to the address of the first element

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

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

 6
Author: Maniero, 2019-09-11 11:32:21