Home java Questions on the transmission of parameters in Java

Questions on the transmission of parameters in Java




Hello! Riding with C # learned that in Java all parameters are transmitted by value.

1) The link refers to an existing object, and if one link referred to it, now there are two of them?

2) A new object is created in the heap, and within the method of the method code you work with a newly agreed object. At the same time, after the completion of the method, the initial object remains not changed? Created by the object in the method has already become on taking into account the garbage collector?

After reading a couple of articles about the transmission of parameters in Java, I decided to experiment.
There is your class with a string field and two methods. The first method changes the field of the object, and the second changes the value of the copied link.

Here is class:

class myclass {
String S;
Public Myclass (String S) {
  this.s = s;
Public Static Void Changefield (MYCLASS OTHER) {
  Other.s = "**********";
Public Static Void Chargeref (MyClass Other) {
  Other = New MyClass ("----------");

However, for some reason, the changefield method changes the object, while ChangeRef is not.

myclass obj1 = new myclass ("1");
  Myclass.changefield (OBJ1);
  MyClass.ChangeRef (OBJ2);
  System.out.printLN (OBJ1.S);
  System.Out.printLN (OBJ2.S);

Program execution output:


Answer 1, Authority 100%

As you said, a link to the object is transmitted in Java (with the exception of primitive classes of type int, long and etc.) at the same time, it is a memory for this link.

In the changefield method, the object is changed. And you see these changes outside the function.

In the change method, there is a new instance of the class and write a link to it to that memory area that was highlighted for a copy of the reference to the object. So you do not change the original link and the source object.

and yes, when the object is transmitted to the method, an additional link is created, i.e. They are getting two, as you wrote in your question.

Answer 2, Authority 50%

In Java, there is no transmission on the link in the sense that you can change the reference types, but the links themselves indicating the instances of these types are not.

In C #, in fact, everything is exactly the same: the same code that you wrote will work in the same way. When callingField call, you change the instance field, but not an instance itself. And when calling ChangeRef, you are trying to change the link itself, but the links remain unchanged, since in the method you only pass their copies.

Difference between Java and C # will be that C # using the keywords REF and OUT allows you to change the links to objects yourself. In Java, this is not

Answer 3

Full-time Java Letter is worth reading. Everything is transmitted by value. Java has no transfer by reference. That’s just MyClass OBJ1 = New Maclass ("1") means that in OBJ1 is not an object and a link to it. At the same time Long Val = 1l means that in Val stored 1, but Long Val = New Long (1) – will already mean that Val is stored link to the object type Long (not to be confused with a primitive type Long )

Here it is here more https://docs.orcle .com / Javase / Specs / JLS-4.HTML

i.e. Difference between Reference Types and Primitive Types

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions