Java is obviously a cost pass, but needs clarification

Read a lot of answers to the stack overflow on this topic, and also read a lot of blogs. I can safely conclude that Java is passed by value.

But to convince people, I need proof of some kind of language that follows the link.

So can someone give a living example of a language that follows a link, whose example we could link is that java does not follow a link.

+4
source share
4 answers

So can anyone give a live example of a language that follows a link, whose example we could link is that java does not follow a link

Pass-by-ref vs pass-by-value .

void swap(String a, String b) {
    String c = b;
    b = a;
    a = c;
}

void main() {
    String a = "A";
    String b = "B";
    swap(a, b);
    System.out.println(a); // A
    System.out.println(b); // B
}

, main.a , swap.a, "A".

vs # (IDE One), .

void Swap(ref string a, ref string b) {
    string c = b;
    b = a;
    a = c;
}
void main() {
    string a = "A";
    string b = "B";
    Swap(ref a, ref b);
    Console.WriteLine(a); // B
    Console.WriteLine(b); // A
}

main.a swap.a , swap.a main.a.

,

void swap(StringBuilder a, StringBuilder b) {
    String a1 = a.toString();
    String b1 = b.toString();
    a.setLength(0);
    a.append(b1);
    b.setLength(0);
    b.append(a1);
}

void main(){
    StringBuilder a = new StringBuilder("A");
    StringBuilder b = new StringBuilder("B");
    swap(a, b);
    System.out.println(a); // B
    System.out.println(b); // A
}

, , . :

public static void main(String... agv){
    StringBuilder a = new StringBuilder("A");
    StringBuilder b = new StringBuilder("B");
    StringBuilder alsoA = a;
    swap(a, b);
    System.out.println(a); // B
    System.out.println(b); // A
    System.out.println(alsoA); //B
}

vs # (IDEOne)

void Main() {
    string a = "a";
    string b = "b";
    string alsoA = a;
    Swap(ref a, ref b);
    Console.WriteLine(a); // B
    Console.WriteLine(b); // A
    Console.WriteLine(alsoA); // A
}

Java Ranch , .

+4

- , , # ref, pass-by-reference. . :

using System;

public class Program
{
    static void Main()
    {
        string x = "original x";
        string y = "original y";
        Modify(x, ref y);
        Console.WriteLine(x);
        Console.WriteLine(y);
    }

    static void Modify(string px, ref string py)
    {
        px = "modified x";
        py = "modified y";
    }
}

original x
modified y

, px x : x . py y, y ; py y - .

. :

using System;
using System.Text;

public class Program
{
    static void Main()
    {
        StringBuilder x = new StringBuilder("original x");
        Modify(x);
        Console.WriteLine(x);
    }

    static void Modify(StringBuilder px)
    {
        px.Length = 0;
        px.Append("modified x");
    }
}

Modify px - , px. - x px. -. - . , , , , , .

, .

+3

++ . - . , , Java. . , , , .

++ . . , .

#include <iostream>

using namespace std;

class ClassA
{

public:
    int a = 0;

};

void funcByValue(ClassA *param)
{
    param->a = 1;
    param = new ClassA();
}

void funcByReference(ClassA* &param)
{
    param->a = 1;
    param = new ClassA();
}


int main()
{
    ClassA *objectA = new ClassA();

    cout << objectA->a << "\n";     //1
    cout << objectA << "\n";        //0000002B1D355AF0

    //This call changes objectA state
    funcByValue(objectA);
    cout << objectA->a << "\n";     //1
    cout << objectA << "\n";        //0000002B1D355AF0

    //This call changes the object objectA is referencing
    funcByReference(objectA);
    cout << objectA->a << "\n";     //0
    cout << objectA << "\n";        //0000002B1D345610
}
0

Source: https://habr.com/ru/post/1617563/


All Articles