Array and type being created

Asked

Viewed 155 times

4

In Java, when I have the following instruction:

int[] ns = new int[5];

the following doubts arise:

  1. An object of what type is created?
  2. For each value in an index, I call it an instance?
  • 1

    As the title says, an array is created, only with all positions reset, like this: [0,0,0,0,0]. Indexes do not have instantiation because it is a primitive type array and primitive types are not instantiable. If it was a nonprimitive type array (for example String[] arr = new String[5];) would be an array where all indices would be null, because the fact of instantiating the list does not mean that objects of the type supported by it will be instantiated within the array. See proof: https://ideone.com/rBXNuC

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

3

An object of what type is created?

ns will have an object of type array of int and cannot be changed. Note that because it is a type by reference the variable will have a pointer to the real object.

For each value in an index, I call it an instance?

For each value in a location that can be accessed by an index there will be a type instance int. Because it is a type by value, the instance is already right there in the variable, that is, in the variable ns[0] will have a 4 bytes integer, in ns[1] also, and so on.

System.out.println(1);
System.out.println("x");
System.out.println(new Object());
System.out.println(new ArrayList());

I put in the Github for future reference.

The first has an instantiated object of type int and it is used to print. The object originally is in the static part of the code. When called the method println() a copy of it is passed as argument, so the parameter will receive a new instance of int identical to the original. We are sure it is a new instance because the argument value could change smoothly (in this case it does not change because it is a constant) and would not affect the value being used within println(), after all are two completely different instances. There is an instance, is an object, based on primitive type. I will not go into the detail that some implementation may require to do Boxing and the value be copied to the heap, which is inefficient.

The second is very similar. What many people don’t understand is that the literal string is actually a new String("x"). The guy string looks like a primitive type because it has value semantics, but in fact it is a type by reference. It will probably be allocated in the heap because of the new, but this is not guaranteed, there can be an optimization and be allocated in the stack (this is just implementation details). Note that in this case there will not even be a de facto allocation, because the string will be in the static part of the code and the required reference will point to this address.

The third creates an object by reference, probably allocated in the heap, and he’s kind of Object. Since there is no structure in it, the allocation will be empty (but it consumes memory space anyway (if I’m not mistaken 20 bytes).

The fourth creates an object in the same way, and it will be derived from Object. Don’t confuse Object with Object. One is a word we use to designate presumably material things, the other is the name of a root class used in some object-oriented languages, as is the case with Java.

Behold Memory allocation in C# - Value types and reference types. In Java it’s the same.

  • 2

    haverá uma instância de tipo int. - but int is a primitive type not instantiable, as can be considered an instance?

  • And why do you think that?

  • Unused new to start a primitive variable, the cited example itself shows this, when the array is created with zeros in the positions.

  • And what the new has to do with creating an instance?

  • And how is it possible to have an instance of something in java if nobody gave new at some point?

  • Define instance.

  • The object-shaped representation of a given class. But what I wonder is if each position of the array is considered an instance, so a null type could also be considered an instance, given that a nonprimitive type array is created with null positions.

  • Where is that definition? That’s why you have to read what I say about types by value and by reference. These are separate things that cannot be treated as if they were the same thing. One instance is in the storage location itself, the other there is a pointer in the storage place and the instance elsewhere. In the array, and not elsewhere, has an object when using a int? You have an object when you use one Integer?

  • "For each value in a location that can be accessed by an index there will be an instance of type int." You meant an instance of type int array?

  • @Alexandres.V.Oliveira no, I said that, I did not mean anything else, nor would it make sense to say what you are thinking.

  • @Maniero Sorry I don’t follow your reasoning, but how can there be an instance of a primitive type?

  • @Maniero then, basically, an instance is an object in memory and everything that has methods and attributes can be called an object, even if it is a primitive type variable like int, it would still be an instance for an object in memory, this would be the reasoning?

  • 1

    @cat doesn’t even need to have methods to be an object. People cling too much to a capenga definition given by OO. And I began to abhor the use of the term attribute once and for all. The reasoning is this same, I would only change a little the text because instance and object are synonymous there. There is instance of a type, based on a definition (class, structure, prototype, etc.), the instance is an object, the instantiation creates an object. The next version of Java will knock a lot of people off the horse because they believe in things that don’t exist.

  • @Alexandres.V.Oliveira if there is no instance then what is the value of this type stored in memory?

  • @Maniero wrote that you do not need to have methods to be an object, and that you now abhor the use of the term attribute. Could you explain to me how you differentiate a primitive type from an object? I’m new to the subject, so, not to ask too much, try to use a more understandable didactic.

  • @Alexandres.V.Oliveira I could, but the subject extended, ask a new question that I answer.

  • @Maniero agree with the first answer (the created object is of the int array type). But I disagree with the second answer, which says that the value that occupies an index is an instance of int. I understand your thinking that if a so-called object-oriented language, then all data would be objects, and regardless of where it is allocated. But my question is in the context of the Java programming language, and in Java today not all data is considered objects, and so such data is called primitive types. And so I’m still waiting for an answer to my second question.

  • @Alexandres.V.Oliveira you can disagree with whatever you want. I’m talking about something I’ve studied my entire life, which I’ve applied for 35 years, and I’m talking about it based on all the existing literature, not in my head. I regret that you have not yet understood and that you have difficulty interpreting text, if you want to continue learning wrong is your right. Other people who feel the same way that you have changed your mind. All languages, including Java, including non-OO languages, have all data stored in objects. Drop the bone. In OOP the reference types are derived from Object.

  • 1

    I will repeat what I have already answered in https://answall.com/a/274396/101. Object and object are distinct things. There is a clear grammatical and even spelling difference which makes them have distinct meanings. You think it’s the same thing, many people think it is, but it’s not. You don’t want an answer to learn, you want an answer that confirms what you think it is. I won’t do it because it’s letting you keep fooling and I’d be fooling other people.

  • 2

    @Maniero I agree with your thinking, but stop the arrogance in your comments and/or answers, and then yes, I will gladly read them to learn from them. Indeed, with as much experience as you wrote, you should know that when discussing a subject it is essential that education and respect be present.

Show 15 more comments

Browser other questions tagged

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